Page 596 - Introduction to Programming with Java: A Problem Solving Approach
P. 596
562
Chapter 14 Exception Handling
while (!xStr.equalsIgnoreCase("q"))
{
yStr = stdIn.next();
try
{
}
// end while
}
{
}
x = Integer.parseInt(xStr);⎫ ⎬
y = Integer.parseInt(yStr);⎭ catch (NumberFormatException nfe)
System.out.println("Invalid entry: " + xStr + " " + yStr
+ "\nMust enter integer space integer.");
line.plotSegment(x, y);
System.out.print("Enter x & y coordinates (q to quit): ");
xStr = stdIn.next();
These statements should be inside a try block.
Figure 14.3
First attempt at improving the LinePlot program’s while loop Apago PDF Enhancer
while (!xStr.equalsIgnoreCase("q"))
{
yStr = stdIn.next();
try
{
x = Integer.parseInt(xStr);
y = Integer.parseInt(yStr);
line.plotSegment(x, y);
}
// end while
}
catch (NumberFormatException nfe)
{
}
System.out.println("Invalid entry: " + xStr + " " + yStr
+ "\nMust enter integer space integer.");
System.out.print("Enter x & y coordinates (q to quit): ");
xStr = stdIn.next();
This statement should be inside the try block, not after the try-catch structure.
Figure 14.4
Final version of the LinePlot program’s while loop