• Post Reply Bookmark Topic Watch Topic
  • New Topic

Parsing String to Integer if it is a Number or keeping as String itself  RSS feed

 
Sujai Kaarthik
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,
I have a situation where I will get a String and need to check whether that String holds a number or a String and need to store it in another variable.
For example my input value may have "00002888" or "W-23HSG". The first String has number, so I can parse it to 2888 using Integer.parseInt("00002888"). What can I do for the other case?

I need to write like this:



I am getting number format exception. I know why it throws that exception. But, what should I do to overcome this exception?
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you get a NumberFormatException that would indicate the string is not a number and you can store it in the "other variable"
 
Sujai Kaarthik
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply Maneesh.

How to check that?
When I use Integer.parseInt method, it throws that exception and my application stops. Without using that how can I check that whether it will throw that exception or not?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, you dont want to catch Exception? then go through whole String character by character and check it has a string character or not .
 
Sujai Kaarthik
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Seetharaman. I didnt thought of that really. Thanks very much for reminding me. Now I am able to solve
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sujai Kaarthik wrote:Thanks Seetharaman

You are welcome
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sujai Kaarthik wrote:Thanks for the reply Maneesh.

How to check that?
When I use Integer.parseInt method, it throws that exception and my application stops. Without using that how can I check that whether it will throw that exception or not?


So put it in a try catch block
In pseudo code

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sujai Kaarthik wrote:I have a situation where I will get a String and need to check whether that String holds a number or a String and need to store it in another variable.
For example my input value may have "00002888" or "W-23HSG".

Sounds to me like that's where your problem is. Why do you need to do it that way? If it's something you have no control over, then there might be some justification for it; but if not, it seems to me that you're trying to put two different things into one slot.

BTW, if you absolutely must, you could do something like:but personally I'd try to avoid the situation if I could.

Winston
 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem with that regular expression is the lack of bounds checking. If I would pass "12345678901234567890" it would match the regex but the number represented in the String is too large for both int and long.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote: . . . If I would pass "12345678901234567890" it would match the regex but the number represented in the String is too large for both int and long.
BigInteger?

If you are doing arithmetic with those values, then they need to be parsed to an int, a long or even BigInteger. If you are not performing arithmetic, why not keep them all as Strings?
Why not create a CodeNumber class with a String field and a Type enum with values like INTEGER HYPHENATED_STRING etc. You can identify them with regular expressions.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:The problem with that regular expression is the lack of bounds checking.

True. It was more for illustration than anything else; and I still think that the main problem is actually having two possible types of input in the first place. Seems 'smelly' to me.

Winston
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:
Sujai Kaarthik wrote:Thanks for the reply Maneesh.

How to check that?
When I use Integer.parseInt method, it throws that exception and my application stops. Without using that how can I check that whether it will throw that exception or not?


So put it in a try catch block
In pseudo code



I don't think exceptions should be used to perform logic. An exception is something to avoid if we can help it.
 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While usually I agree, NumberFormatException is one of the few exceptions I make. While usually you should try to perform some checks to prevent the exceptions, there is not really such a check for Integer.parseInt and its counterparts in the other wrapper classes. If these classes would have methods public static boolean isInt(String s) etc I would gladly never catch NumberFormatException anymore. Until that time I'll have to make do with catching NumberFormatException, or use a regular expression that works for every possible value of int. That's not going to be a short regex, so NumberFormatException it is.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!