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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Please do not repeat other people's answers allahabad. How many times do i have to say this to you?
ASKER
thank u :)
knew it was probably easy!
thanks
Gareth
knew it was probably easy!
thanks
Gareth
ASKER
its saying:
incompatible types
found : int
required: java.lang.String
String MapLinkTest = Integer.parseInt(MapLinkIn );
Thanks G
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. :-)
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. :-)
ASKER
ok, i now have:
try
{
int MapLinkTest = Integer.parseInt(MapLinkIn );
System.out.println(MapLink Test);
}
catch(NumberFormatExceptio n e)
{
ProjectDBase.EditOutputTex tField.set Text("Map Link Error - Not in range, try again");
}
it doesnt throw the exception when i put XXX into the input, ideas?
G
try
{
int MapLinkTest = Integer.parseInt(MapLinkIn
System.out.println(MapLink
}
catch(NumberFormatExceptio
{
ProjectDBase.EditOutputTex
}
it doesnt throw the exception when i put XXX into the input, ideas?
G
Are you sure? Try:
catch(NumberFormatExceptio n e)
{
e.printStackTrace();
ProjectDBase.EditOutputTex tField.set Text("Map Link Error - Not in range, try again");
}
catch(NumberFormatExceptio
{
e.printStackTrace();
ProjectDBase.EditOutputTex
}
ASKER
ooh yes sorry CEHJ but thanks for that idea made me realise i was overwriting later on!
thanks again for the quick answer!
Gareth
thanks again for the quick answer!
Gareth
String mapLink = Integer.parseInt(MapLinkIn
}catch(NumberFormatExcetio
// not a number
System.out.println("This is not a number");
}