0

I want the country codes are integer that input by the user. I want an error message to be show when user inputs a code which is not an integer. How can I do this? The program is to ask user to enter country name and country code. In which user will input the country code. But if user inputs a character I want a message to be shown saying Invalid Input.

System.out.println("Enter country name:");                     
countryName = in.nextLine();
System.out.println("Enter country code:");            
int codeNumber = in.nextInt(); 
in.nextLine();
1

5 Answers 5

1

If the input is not an int value, then Scanner's nextInt() (look here for API) method throws InputMismatchException, which you can catch and then ask the user to re-enter the 'country code' again as shown below:

  Scanner in = new Scanner(System.in);
  boolean isNumeric = false;//This will be set to true when numeric val entered
  while(!isNumeric)
     try {
        System.out.println("Enter country code:");
        int codeNumber = in.nextInt(); 
        in.nextLine();
        isNumeric = true;//numeric value entered, so break the while loop
        System.out.println("codeNumber ::"+codeNumber);
  } catch(InputMismatchException ime) {
     //Display Error message
     System.out.println("Invalid character found,
            Please enter numeric values only !!");
     in.nextLine();//Advance the scanner
  }
Sign up to request clarification or add additional context in comments.

2 Comments

why new Scanner(System.in) is inside the while loop?
Yes, no need, fixed it
0

One simple way of doing it, is reading a line for the numbers as you did with the name, and then checking witha Regex (Regular Expression) to see if contains only numbers, with the matches method of string, codeNumber.matches("\\d+"), it returns a boolean if is false, then it's not a number and you can print your error message.

System.out.println("Enter country name:");                     
countryName = in.nextLine();
System.out.println("Enter country code:");            
String codeNumber = in.nextLine(); 
if (codeNumber.matches("\\d+")){
    // is a number
} else {
    System.out.println("Please, inform only numbers");
}

Comments

0

You can do something like this, by first getting the input as a string, then try to convert the string to an integer, then outputs an error message if it can't:

String code= in.nextLine();
try
        {
          // the String to int conversion happens here
          int codeNumber = Integer.parseInt(code);
        }
catch (NumberFormatException nfe)
        {
          System.out.println("Invalid Input. NumberFormatException: " + nfe.getMessage());
        }

Comments

0

You could instead check hasNextInt then call nextInt

int codeNumber;

System.out.println("Enter country code:");

if(in.hasNextInt())
{
    codeNumber = in.nextInt();
}
else
{
    System.out.println("Invalid Code !!");
}

Comments

0

If you are creating your own custom exception class, then use regex to check if the input string is an integer or not.

private final String regex = "[0-9]";

Then, check if the input follows the regex pattern.

if (codeNumber.matches(regex)) {
    // do stuff.
} else {
    throw new InputMismatchException(codeNumber);
}

You can use build in InputMismatchException if you are not creating your custom exception handler.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.