-
How to create file objects?
Import java.util.io; File file = new File(“TestFileClass.java”);
-
How to read data from a file?
- Create file object with RELATIVE path (“src/a.txt”); not (“/home/me/src/a.txt”); ...
- Then import Scanner input = new Scanner(file); ...
- While(input.hasNext()) { String firstName= input.next(); }
-
How to close a file after you’re finished
scannerName.close(); ... input.close();
-
How to write data to a file?
- After creating file object,
- PrintWriter output = new PrintWriter(file); ....
- Then... output.print(“John T Smith “); ...
- Don’t forget to close writing with output.close();
-
Benefits of exception handling?
Allows program to continue past runtime errors. Must import java.util.*;
-
How is a try catch block used
- It surrounds code that can potentially throw an exception,
- try { // code like num1/num2}
- catch (ArithmeticException ex)
- { sysout(“error canhot be divided by zero”);
- sysout(input.next()); // clear data}
- ALL EXCEPTION HANDLING MUST HAVE java.util.*;
-
Trying to put a double in an int value
InputMismatchException
-
Three major exception types
- System errors
- Exceptions
- Runtime errors
-
System errors
Can’t do anything anout them
-
Exceptions
- Include runtime exceptions, class not found exceptions, etc...
- Can all be caught and handled
-
Runtime exceptions
- Subset of exceptions,
- Result of bad casting, accessing out of bounds array, numeric errors,
- ArithmeticException, NullPointerException, IndexOutOfBoundsException, IllegalArgumentException, many more.
-
NullPointerException
Thrown if you try to access an object through a reference variable before an object is assigned to it
-
Major pieces of exception handling
Declare exception, throw exception, catch exception
-
Catching general exceptions
- Use on top of other possible exceptions,
- catch (Exception ex) {
- handle; }
-
How does finally block work in try catch? Difference between catch?
Always executed whether exceptions are thrown or not, where catch is only executed if exceptions are thrown- finally will still execute
-
Hi to throw and catch an ArithmeticException without try catch?
- Use if statement...
- if (num2 != 0) {
- ans = num1/num2; } else {
- sysout(“cannot divide by zero”); }
-
Throw catch a null pointer exception without try catch
- if (rVar != null)
- sysout(rVar);
- else
- sysout(“rVar is null”);
-
Why use a do while
When the program needs to execute at least once before checking continuation condition
-
Why use while loop
When number of repitions are unknown, ie. until a zero is entered
-
Why use a for loop
When the number of repetitions are known
-
How does a nested loop execute
Every iteration of the outer loop executes all iterations of the inner loop
-
What does break and continue mean
- Break- breaks out of loop,
- Continue- skips rest of current iteration and continues loop
-
How to avoid using break
Include value of Boolean condition in the continuation condition of the loop
-
How to avoid using continue
Create an if statement that addresses the instance
-
Purpose of using methods
To create actions that are reusable
-
String variables can hold what type of values
Char, Unicode
-
Why use built in math functions
- More accurate (Math.PI)
- Truly random numbers (Math.rand())
- Symbols not on keyboard (Math.sqrt())
-
How to get the pattern of 5 asterisks, 4 asteriks... all the way to one
- for (int i = 5; i > 0; i- -) {
- for (int j = 1; j <= i; j++) {
- sysout(“*”);
- }
- system.out.println();
- }
|
|