?
Solved

Java String check - easy question probably

Posted on 2003-03-10
9
Medium Priority
?
169 Views
Last Modified: 2010-03-31
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
0
Comment
Question by:PowerMaxUK
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 200 total points
ID: 8102373
If

Integer.parseInt(MapLinkln)

doesn't throw a NumberFormatException, then you're fine
0
 
LVL 3

Expert Comment

by:allahabad
ID: 8102397
try {
String mapLink = Integer.parseInt(MapLinkIn);
}catch(NumberFormatExcetion e){
// not a number
  System.out.println("This is not a number");
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8102430
Please do not repeat other people's answers allahabad. How many times do i have to say this to you?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:PowerMaxUK
ID: 8102446
thank u :)

knew it was probably easy!

thanks
Gareth
0
 

Author Comment

by:PowerMaxUK
ID: 8102482
its saying:

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

Thanks G
0
 
LVL 35

Expert Comment

by:girionis
ID: 8102562
 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. :-)
0
 

Author Comment

by:PowerMaxUK
ID: 8102688
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
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8102843
Are you sure? Try:

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

Author Comment

by:PowerMaxUK
ID: 8102942
ooh yes sorry CEHJ but thanks for that idea made me realise i was overwriting later on!

thanks again for the quick answer!

Gareth
0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month8 days, 10 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question