Page 595 - Introduction to Programming with Java: A Problem Solving Approach
P. 595

                As you can see, the program’s display is very primitive—it uses text to represent each line segment. In a real line-plotting program, you’d use Java’s lineDraw method to display the line. That’s what we do in the GUI section at the end of this chapter. But for now, we’ll keep it simple and use a text-based display rather than a GUI-based display. That way, we can maintain focus on this chapter’s primary topic, exception handling.
Using “q” as a Sentinel Value
In the past, when you entered numbers inside a loop, you often terminated the loop with a numeric sentinel value. This program employs a more elegant solution because it allows a non-numeric “q” as the sentinel value. How can you read in numbers and the string “q” with the same input statement? Use strings for both types of input—for the “q” and also for the numbers. For each number input, the program converts the num- ber string to a number by calling the Integer class’s parseInt method.
We described the Integer class’s parseInt method back in Chapter 5. The parseInt method at- tempts to convert a given string to an integer. That should sound familiar; in the LuckyNumber program, we used the Scanner class’s nextInt method to convert a given string to an integer. The difference is that the nextInt method gets its string from a user and the parseInt method gets its string from a passed-in parameter. If the passed-in parameter does not contain digits and an optional minus sign, the JVM throws a NumberFormatException . NumberFormatException is in the java.lang package. Since the JVM automatically imports the java.lang package, your program doesn’t need an explicit import to refer to a NumberFormatException.
Input Validation
Apago PDF Enhancer
Note how the LinePlot program calls stdIn.next to read x coordinate and y coordinate values into xStr and yStr, respectively. Then the program attempts to convert xStr and yStr to integers by calling Integer.parseInt. The conversions work fine as long as xStr and yStr contain digits and an op- tional minus sign. But what happens if the user enters a non-integer for xStr or yStr? With invalid input, the program crashes, like this:
Sample session:
Enter x & y coordinates (q to quit): 3 1.25
Exception in thread "main" java.lang.NumberFormatException: For
input string: "1.25"
...
To deal with this possibility, let’s rewrite the while loop in the main method of Figure 14.2 so that it in-
14.4 Line Plot Example 561
      cludes input validation using a try-catch mechanism. The first step is to identify the dan- gerous code. Can you find the dangerous code? The two parseInt method calls are dangerous in that they might throw a NumberFormatException. So let’s put those two statements into a try block and add a matching catch block, as shown in Figure 14.3.
Look for potential problems.
        Do you see any logic errors in Figure 14.3’s while loop? What happens if there’s invalid input? A NumberFormatException object is thrown and caught, and then an error message is printed. Then line.plotSegment executes. But you wouldn’t want to print the line segment if the input values were messedup.Toavoidthatpossibility,movetheline.plotSegment(x, y);linetothelastlineinthe try block. This way, it gets executed only if the two parseInt method calls work properly. Figure 14.4 shows the final version of the LinePlot program’s while loop.


















































































   593   594   595   596   597