Assume all necessary imports have been made, an appropriate plotSegment method h
ID: 3534895 • Letter: A
Question
Assume all necessary imports have been made, an appropriate plotSegment method has been defined, and x and y have been declared with the statement, int x, y;.
a) What's still wrong with the following code fragment?
while (!xStr.equalsIgnoreCase("q"))
{
yStr = stdIn.next();
try
{
x = Integer.parseInt(xStr);
y = Integer.parseInt(yStr);
}
catch (NumberFormatException nfe)
{
System.out.println("Invalid entry: " + xStr + " " + yStr
+ " Must enter integer space integer.");
}
line.plotSegment(x, y);
System.out.print("Enter x & y coordinates (q to quit): ");
xStr = stdIn.next();
} // end while
Explanation / Answer
//you should try like this
while (true)
{
System.out.print("Enter x & y coordinates (q to quit): ");
xStr = stdIn.next();
if(xStr.equalsIgnoreCase("q"))
break;
yStr = stdIn.next();
try
{
x = Integer.parseInt(xStr);
y = Integer.parseInt(yStr);
}
catch (NumberFormatException nfe)
{
System.out.println("Invalid entry: " + xStr + " " + yStr
+ " Must enter integer space integer.");
}
line.plotSegment(x, y);
} // end while
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.