Link to home
Start Free TrialLog in
Avatar of PowerMaxUK
PowerMaxUK

asked on

Java String check - easy question probably

not sure if there is a command, maybe there is and i havent found it but.

i have an input: MapLinIn.
i want to make sure that its length is equal to 3. i have done that with line below:

if (MapLinkIn.length()==3)

i now want to make sure that all 3 of the chars in the string are integers
so only say 123 was accepted or 012 etc etc

is it posible to make sure all the chars in the input are integers and if so how :)

many many thanks

Gareth
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of allahabad
allahabad

try {
String mapLink = Integer.parseInt(MapLinkIn);
}catch(NumberFormatExcetion e){
// not a number
  System.out.println("This is not a number");
}
Please do not repeat other people's answers allahabad. How many times do i have to say this to you?
Avatar of PowerMaxUK

ASKER

thank u :)

knew it was probably easy!

thanks
Gareth
its saying:

incompatible types
found   : int
required: java.lang.String
String MapLinkTest = Integer.parseInt(MapLinkIn);

Thanks G
 Change this:

  String MapLinkTest = Integer.parseInt(MapLinkIn);

  to this:

  int MapLinkTest = Integer.parseInt(MapLinkIn);

  Also, as a good programming practice, I recommend you have all variable names start with lowercase letters. :-)
ok, i now have:

try
{
int MapLinkTest = Integer.parseInt(MapLinkIn);
System.out.println(MapLinkTest);

}
catch(NumberFormatException e)
{
ProjectDBase.EditOutputTextField.setText("Map Link Error - Not in range, try again");
}

it doesnt throw the exception when i put XXX into the input, ideas?

G
Are you sure? Try:

catch(NumberFormatException e)
{
e.printStackTrace();
ProjectDBase.EditOutputTextField.setText("Map Link Error - Not in range, try again");
}
ooh yes sorry CEHJ but thanks for that idea made me realise i was overwriting later on!

thanks again for the quick answer!

Gareth