CS115 lecture notes, Spring 1999 Week 9, Wednesday Quiz 12 Solutions ----------------- Two pointers referring to the same object is very different from two pointers referring to different objects, even if the objects contain the same values. Guardians and safe methods -------------------------- On the last assignment, we wrote methods like first and last in a way that was "unsafe", meaning that if the preconditions of the method were not satisfied (by the caller), it would cause an error. // first returns the first character of a string // first only works if s has at least one characters public static char first (String s) { return s.charAt (0); } What conditions will cause first to fail? 1) an empty string 2) null! (an uninitialized String is null, just like other object variables) That's why we used the guardian pattern to insure that the preconditions are satisfied. An alternative is to make the method "safe", meaning that it will not cause an error under any circumstances. // tail returns all but the last two characters of the // String. it is completely bulletproof; there are no // preconditions. public static String tail (String s) { if (s == null) return ""; int len = s.length(); if (len == 0 || len == 1) { return ""; } return s.substring (2, len); } Sometimes it is not clear what the right thing to do is when there is an error: // second returns the second character of a string public static char second (String s) { if (s == null || s.length() == 0) { // then what? } return s.charAt (1); } System objects -------------- System is the name of the built-in class that contains methods and objects used to get input from the keyboard, print text on the screen, and do file input/output (I/0). System.out is the name of the object we have used to print text. When you invoke print and println, you invoke them on the object named System.out. Printing objects ---------------- Interestingly, you can print System.out: System.out.println (System.out); The output is: java.io.PrintStream@80cc0e5 As usual, when Java prints an object, it prints the type of the object, which is PrintStream, the package in which that type is defined, java.io, and a unique identifier for the object. On my machine the identifier is 80cc0e5, but if you run the same code, you will probably get something different. Keyboard input -------------- There is also an object named System.in that has type BufferedInputStream. System.in makes it possible to get input from the keyboard. Unfortunately, it does not make it easy to get input from the keyboard. First, you have to use System.in to create a new InputStreamBuffer. InputStreamReader in = new InputStreamReader (System.in); Then you use in to create a new BufferedReader: BufferedReader keyboard = new BufferedReader (in); The point of all this manipulation is that there is a method you can invoke on a BufferedReader, called readLine, that gets input from the keyboard and converts it into a String. String s = keyboard.readLine (); System.out.println (s); reads a line from the keyboard and prints the result. Throwing Exceptions ------------------- There is only one problem. There are things that can go wrong when you invoke readLine, and they might cause an IOException. There is a rule in Java that if a method might cause an exception, it should say so. The syntax looks like this: public static void main (String[] args) throws IOException { This indicates that main might ``throw'' an IOException. You can think of throwing an exception as similar to throwing a tantrum. Any method that uses the readLine method must declare that it throws IOException. Actually, there is an alternative (the try statement), which we will get to a little later. Error handling -------------- What can you do when you detect an error? Answer depends on what kind of error: 1) external: user or file input is wrong Example: your compiler finds a syntax error in the program you are trying to compile Solution: generate useful error message. If the program is interactive, give the user another chance. Otherwise, just quit. 2) internal: something weird has happened, but it's recoverable Example: you invoke indexOf, but the character does not appear in the String. Solution: return special value (sometimes tricky) Alternative: throw an exception 3) internal: the program has done something wrong Example: a method checks its precondition and finds that the caller has failed to live up to the bargain. Solution: print message that will help the program locate the source of the error Alternative: throw an exception