• 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

How to convert String to int ?

 
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everybody.

I have a small problem. I want to convert a code in String data type to int.
I am reading a csv file using StringTokenizer. the second column contains the code below,in String data type:


i am converting that to int using following code:

During this string to int conversion, i am geting NumberFormat Exception.

Can anyone help me in this regard?
Thanks in advance.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> During this string to int conversion, i am geting NumberFormat Exception.

That's because you're trying to convert text that does not consist of numbers into an int. I guess you are getting the error in the line

Try changing it to the following, so that you can see what you're actually trying to convert to an int:
 
Anu satya
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Jesper,

As you said, i am geting the error during convertion from String to int.
That is because, the String contains special character '&'. I Think, that can't be converted into int. but, i want the entire code as int to proceed further in my program. How to do it ??

Can anyone solve my problem please?
i am giving simple java program to do this convertion.

Please help.
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are posing a mysterious problem. I can't imagine what int value you expect the String "P II + 3630-146 & 3630-337" to be parsed to. Perhaps if you wrote what int value you expected this to produce, forum members would be able to suggest something. Do you expect (3630-146)*(3630-337)?
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you're looking for a numeric identifier for that String then you can use the hashCode() method. This just returns an integer "hashcode" for the string. This code will appear to be an arbitrary value for your string. The only thing you're guarenteed concerning this hashcode is that two equal strings will have the same hashcode.

This is a one-way function. You can't start with the hashcode and get back to the String.

_M_
 
author
Posts: 4335
39
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem here is that we're using the word 'convert' when what Ashvini is really talking about should be describe with the word 'parse'.

Converting a string into a number implies a 1-to-1 type of relationship (most of the time). But parsing, implies 1-many relationships that can vary depending on your parsing engine. For example, what order of operation do you use in your example? If you go left to right + would be evaluated before & but if you use scientific notation, & would be evaluated before +.

What you want to do is either write your own parser (your 'small' then becomes a big problem as writing a numeric parser is non-trivial) or find a numeric equation parser out there that most closely matches your data and use it by converting your data. For example, other parsers might interpret Pi as "pi" or have special symbols like "@pi".
 
Scott Selikoff
author
Posts: 4335
39
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh and as I side note, parsing is never "simple" as you say your program is, even using someone else's parser. Speaking as someone whose done a ton of mathematical programming as well as written a numeric parser from scratch, it can be *very* difficult.

If you think about it, MatLab and Mathematica make millions based solely on their ability to parse equations. In fact, with proper licenses you can actually use their engines to parse your data, but again this is not simple.
 
reply
    Bookmark Topic Watch Topic
  • New Topic