Because null means no object has been assigned to the

Info icon This preview shows pages 10–12. Sign up to view the full content.

- Because null means no object has been assigned to the variable, that means there is no way to run a method or access a data member from a null variable - null.method() -> Null Pointer Exception - null.variable -> Null Pointer Exception Object foo; if (foo != null) { sout(foo.toString()); } else { sout("Something is wrong: foo is null"; } -------------------------------------------------------------------------------- -------------------------------- 12/4/2014 - Always use forward slashes - Java is platform independent so there is no need to use Windows file paths with backslashes; C;\hello.txt - Always specify file paths using forward slashs; /hello.txt - Reading from a File - import java.util.Scanner; - Creat a new Scanner with a File instance - Scanner commands to test if there is data waiting; hasNext(), hasNextLine(); EXAMPLE import java.util.Scanner File myFile = new File ( "C:/Home/somefile.txt"); Scanner input = new Scanner(myFile); String name = input.next; int age = input.nextInt(); input.close;
Image of page 10

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

- Next - The next method reads a string that is delimited by a space - Reading Characters - Set the Scanner delimiter to "" - Scanner in= new Scanner (myFile); in.useDelimiter(""); - Testing Characters - The character class contains static methods from determining the kind of character - Character.isDigit (myChar) isDigit 1,2,3,4 isLetter A,B,C,a,b,c isUpperCase A,B,C isLowerCase a,b,c, isWhitespace - Scanner is Flexible new Scanner ( x ) System.in <- Reads from keyboard File <- reads from a file String <- parses string data URL <- reads from a web page - Scanning a String - String myString = "Mary had a little lamb"; Scanner scan = new Scanner (myString); while (scan.hasNext()){ String myWord = scan.next(); System.out.println(myWord); } - Writing to a File import java.io.PrintWriter; A PrintWriter instance contains the same print, println, printf - Exception Handling Exceptions are a way of passing information up the call chain to another part of the program Use the THROW statement to signal an exceptional condition When you throw an exception, processing continues in an EXCEPTION HANDLER A way to communicate between methods - Use-Case: Argument Validation public double debit (double amount){ if (amount > balance){ throw new IllegalArgumentException (Amount exceeds Balance"); } balance -= amount; return balance; } - Catching Exceptions - Place the statements that can cause an exception inside a TRY BLOCK - Place the statements that handle the exception inside a CATCH CLAUSE - Try blocks can have multiple catch clauses try { Scanner scan = new Scanner (inputFile_; String input = scan.next(); process(input); } catch (FileNotFoundException el){ Sout ("File not found"); } catch (IOException e2 ){ Sout (stuff) }
Image of page 11
Exception Categories - FATAL ERRORS - Descend from type Error - UNCHECKED EXCEPTIONS - Descend from RuntimeException. These errors do not need to be caught or declared with throws. - CHECKED EXCEPTIONS - Must be either caught or declard with throws public String readData (String filename) throws FileNotFoundException, NumberFormatException The Finally Clause - is always excuted when control exits from a try-block or catch-clause - Executed even if no exception is thrown PrintWriter out = new PrintWriter (filename); try { writeData (out); } Throw Early, Catch Late Throw an exceptins as soon as a problem is detected. Catch it only when the problem can be handled. try (Scanner in = new Scanner (file)){ while(in.hasNext()){ Sout(in.next()); } }
Image of page 12
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern