• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

How to find if a String is numeric?

 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can I analyze the args[0] input on main and find if it is numeric before using it?
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Cindy,
Java 2 has a static method of the Double class called parseDouble that takes a String argument and returns a double.
If the String argument cannot be converted into a double, it throws a NumberFormatException.
If you put the Double.parseDouble("5.6") in a try block then an Exception will be thrown to your catch block. If the parse was successful, no exception will be thrown.
Let me know if that was too complicated or if you have found an easier way to do it (That would be great).
Hope that helps.
Paul
 
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could try
whatever = Double.parseDouble( args[0] );
which would throw a NumberFormatException if it wasn't a number.
 
Grant Crofton
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great minds...
 
Paul Caudle
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's an example:
in a method somewhere:
String numWord = JTextField1.getText();
double num;
try{
//Attempt to convert to a number
num = Double.parseDouble(numWord);
}
catch(Exception e){
//tell the user if it wasn't a number
JOptionPane.showMessageDialog(this, "Not a valid Number");
//exit the method
return;
}
//here you can do whatever it is you were going to do with that number

That might shed some light on the convoluted explanation from before
Paul
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this:

hope that helps.
 
Paul Caudle
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
that's funny, Grant...agreed about the minds, too
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Both these methods are great! Thanks - it just seems such a common place thing to do that I am amazed that the String class doesn't have an isNumeric method.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic