File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to convert String to int ?

 
Anu satya
Ranch Hand
Posts: 146
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • 0
  • Mark post as helpful
  • send pies
  • 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)?
 
Mike Noel
Ranch Hand
Posts: 108
  • 0
  • Mark post as helpful
  • send pies
  • 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_
 
Scott Selikoff
author
Saloon Keeper
Posts: 3890
15
Eclipse IDE Flex Google Web Toolkit
  • 0
  • Mark post as helpful
  • send pies
  • 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
Saloon Keeper
Posts: 3890
15
Eclipse IDE Flex Google Web Toolkit
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic