Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Parseint

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've got an array of type string
for example,
String array[]={"b", "2", "a", "2.4"}
I want a for loop to access this array and perform one action if the string is a long integer, or another action if the string is an alphabet non-integer.
Now so far I gather that I should use parseLong or parseInt to get the integer value.
I know that if I use these methods on the string "b" I'm going to get a NumberFormatException and it won't continue to check the remaining strings for type Int.
How do I get around this? (perhaps a Try...catch?)
like this:
 
Ranch Hand
Posts: 1179
Mac OS X Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One way could be this sample:

Rene
[ July 28, 2002: Message edited by: Rene Larsen ]
 
nate saider
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whoa.
That's pretty neat. I tried it, but I would still need to convert from a string to a number before applying that if statement, otherwise it checks if the string is a number and proves false
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unfortunately the new Double(String) constructor throws a NumberFormatException rather than creating a NaN representation, if the input string is not a valid number format. Nate, your first idea was right - use Long.parseLong (or Double.parseDouble(), or Integer.parseInt() if those are more appropriate) and simply catch the NumberFormatException if it's thrown. There are other ways, but I'm pretty sure this is simplest.
 
nate saider
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jim. That's what I ended up doing.
Always good to have validation that there was no simpler solution.
 
When people don’t understand what you are doing they call you crazy. But this tiny ad just doesn't care:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic