how to analyze the String input?

The program should accept integers but not the sentences for example

the user enters "Hi how are you"

the program should say"Sorry, please enter the integers"

if the user enters "123454545"

the program should work normally

The main aim is to differentiate between sentences and integers

[323 byte] By [imranmehmooda] at [2007-10-3 2:59:11]
# 1

You can use Exception handling to do this

for example

String userInput;

int number;

try

{

number = Integer.parseInt(userInput);

}

catch(NumberFormatException nex)

{

System.err.println("please enter integer");

}

JBNa at 2007-7-14 20:48:45 > top of Java-index,Java Essentials,New To Java...
# 2
Depends on the requirements. If it's legal for the user to enter "123456789012345", then you'll still get an exception with the Integer.parseInt() solution. Same for "1.23".What do you consider a valid number?
CeciNEstPasUnProgrammeura at 2007-7-14 20:48:45 > top of Java-index,Java Essentials,New To Java...