I have it taking a number in the range from 1000 to 999999 also allowing for a comma. I now need to validate the code and provide the appropriate error messages.
import java.util.Scanner;
public class ConsoleReader {
public static void main(String[] args) {
// TODO Auto-generated method stub
// get a connection to the keyboard
Scanner in = new Scanner(System.in);
// ask the user for input
System.out.print("Please enter an integer between 1,000 and 999,999: ");
// read user's input
String number = in.next();
// get the first part (no comma or thousands)
String firstPart = number.substring(0, number.length() - 4);
// get last three digits
String lastThreeDigits = number.substring(number.length() - 3);
// print the two without the comma
System.out.println(firstPart + lastThreeDigits);
}
{
if (in.hasNextInt())
{
String number = in.next();
}
else
{
System.out.println("Please try again.");
}
}
}