I am just playing with Java.I'm trying to force my program to only accept 3 digit numbers. I believe I have successfully done this using a while loop (please correct me if I'm wrong). But how do I go about printing an error statement if the user enters a string. eg: "abc".
My code:
import java.util.Scanner;
public class DigitSum {
public static void main(String[] args) {
Scanner newScan = new Scanner(System.in);
System.out.println("Enter a 3 digit number: ");
int digit = newScan.nextInt();
while(digit > 1000 || digit < 100)
{
System.out.println("Error! Please enter a 3 digit number: ");
digit = newScan.nextInt();
}
System.out.println(digit);
}
}