Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Java String check - easy question probably

Posted on 2003-03-10
9
Medium Priority
?
172 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
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
Technology Partners: 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month14 days, 20 hours left to enroll

577 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