Final Flashcards
(22 cards)
How to use a try/catch?
try
{
} catch(exceptionclass objectofexceptionclass) {
}
Resources that have to be closed you can use:
Using this it closes it before exception is passed to handler
Try(PrintWriter outfile = newPrintWriter(“pets.txt”); { outfile.println(“Chloe”);
}
//outfile.close() occurs after
__________ block always excecutes
finally
If an exception occurs in a finally block is is raised to it:
Handler
If clean other then close method is required, use:
Finally block
Finally block:
try
{
}
catch
{
} finally { Cleanup //occurs whether or not exception //occurs }
If there is no input when nextInt or nextDouble is called it was cause what exception?
No such element exception
I’m order to avoid NoSuchElementException use:
hasNextInt method
How to use hasNextInt() method:
Example:
If(in.hasNextInt())
{
int value = in.nextInt();
}
How to convert strings to ints?:
int year = Integers.parseInt(“2018”);
double price = Double.parseDouble(“10.99”);
How to use a delimiter?
Scanner in = new Scanner(. . .); in.useDelimiter(“, “);
Separates at every “, “
How to process a file:
while(in.hasNext()) { String input = in.next(); System.out.println(input); }
An array collects collects a sequence of values of the ________ data type
Same
General format of array:
Ex:
double[] values = new double[10]
By Default each number is 0.0
How can you input data such as 1,2,3,4,5 into an array:
int[] example = { 1, 2, 3, 4, 5};
If you have to use array.length do not use:
Parentheses
double[] values = new double[10];
System.out.println(“index 1: “ + values[4];
values[4] = 35.0;
System.out.println(“index 2: “ + values[4];
What is the output?
index 1: 0.0
Index 2: 35.0
To grow an array that has run out of space use:
Array.copyOf method
Array.copyOf method _________ the length of an array
Doubles
With a partially filled array, you need to remember:
How many elements are filled
a _________ belongs to the class, motto any of the objects of the class
Static variable
All objects created from the class have access to the _________ static variable
Same