import java.util.Scanner; /** Error.java - Let's look at error checking in Java. * In other words, verify the user's input is valid. */ public class Error { public static void main(String [] args) { Scanner kbd = new Scanner(System.in); // We need to declare the input variable before, not inside, the loop! int n = 0; boolean needInput = true; while (needInput) { System.out.print("Enter a positive integer: "); n = Integer.parseInt(kbd.nextLine()); if (n > 0) needInput = false; else System.out.println("Sorry, I don't understand. Try again."); } System.out.println("You entered " + n); } }