-1

What can I do to detect string input as an error, where the user has to enter an int value? What is the condition I can use to detect whether the user has entered a string value?

Scanner input = new Scanner(System.in); 
int characterNum = input.nextInt();

do{

 if ((characterNum < 0)&&(characterNum > 12)){
 System.out.println("Invalid Input, Try again");

  }

 characterNum = input.nextInt();

 }while((characterNum < 0)&&(characterNum > 12));
7
  • What I understood is, you are trying to get user input but you want to make sure it is a numeric value? Commented May 3, 2018 at 7:45
  • 2
    Look at this stackoverflow.com/questions/6456219/… Commented May 3, 2018 at 7:45
  • can you put your code to see how are you trying to do ?? Commented May 3, 2018 at 7:46
  • @ElSheikh yes exactly Commented May 3, 2018 at 7:46
  • @S.Liyanage check David's comment Commented May 3, 2018 at 7:48

1 Answer 1

-2

By Using instanceof :

Use "s instanceof String" , where s is variable entered by user to check wither it is String or not

Sign up to request clarification or add additional context in comments.

1 Comment

If the OP takes a string as input, and they want to know if it is or is not parsable as an integer, instanceof String is obviously going to be true. If they try and take input as something other than a string, it will be false. Either way, it is not helpful.

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.