wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes How to convert String to int ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to convert String to int ?" Watch "How to convert String to int ?" New topic
Author

How to convert String to int ?

Anu satya
Ranch Hand

Joined: Mar 17, 2005
Posts: 146
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.


With Regards,
Anu
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

> 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:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Anu satya
Ranch Hand

Joined: Mar 17, 2005
Posts: 146
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

Joined: Sep 16, 2005
Posts: 1780
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)?


There is no emoticon for what I am feeling!
Mike Noel
Ranch Hand

Joined: Dec 15, 2005
Posts: 108
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_


Mike Noel
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3703
    
    5

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".


My Blog: Down Home Country Coding with Scott Selikoff
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3703
    
    5

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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to convert String to int ?
 
Similar Threads
Text File to ArrayList for JTextArea?
Doubt about StringTokenizer !!
I/O
Transpose matrix
hast table and unlimited looping