+ 2

How to know that input is string or int

I want int as input in my program but what if someone enter strring as input than. How can i hide the error.

14th Aug 2017, 4:04 PM
Pankaj Bairwa
Pankaj Bairwa - avatar
3 ответов
+ 9
You will be using Try and Catch It's very simple, it goes like this: int input; try{ input = scan.nextInt(); } catch(Exception e){ System.out.println("Please enter a number!"); } So you tell the program to TRY and do something, but if there's an error it should then CATCH the EXCEPTION and execute another code. Here's an example in Playgrounds: https://code.sololearn.com/cr7G4ehQdC72/?ref=app
14th Aug 2017, 4:09 PM
Limitless
Limitless - avatar
+ 14
It's called exception handling, demonstrated perfectly by @Limitless.
14th Aug 2017, 4:19 PM
Hatsy Rei
Hatsy Rei - avatar
+ 1
#@ Pankaj see this this code tells you 4 possible input types 1) if it is an integer 2) if it is a float 3) if it is a char but not an integer 4) if it is a string https://code.sololearn.com/c1Hr3uQYwwEy/?ref=app
14th Aug 2017, 6:10 PM
sayan chandra
sayan chandra - avatar