Java Forum / General / March 2007
Java String to integer conversion not working
ashish.lohra@gmail.com - 19 Mar 2007 22:45 GMT Hello,
I'm am working on converting a String "596" (I'm reading it from a txt file) to an integer using parseInt() however I get NumberFormatException for the input string : "596". I tried to use trim() method before using parseInt on the String but still doens't work.
Also, I tried to get the length of the input string "596" which I read from the file and the length comes out to be 7. How did that happen? I thought the length should be 3.
Please advise.
AL
JohnT - 19 Mar 2007 22:54 GMT On Mon, 19 Mar 2007 14:45:28 -0700, ashish.lohra wrote:
> Hello, > [quoted text clipped - 11 lines] > > AL Can you post your code? What have you tried? Did you do a google search?
ashish.lohra@gmail.com - 19 Mar 2007 23:05 GMT I basically want ot compare an integer in string st[1] with another integer in pc. I tried converting st[1] to integer and comparing and also converting pc to string and then comparing. Doesn't work in either case.
I tried it 2 ways:
if( (data =inst.readLine()) != null ) // reading a line from txt file { String[] st = data.split("\\t"); // split using tab as a delimiter
int st_temp = Integer.parseInt(st[1]); // want to compare integer in string st[1] with pc which is an integer
if( st_temp == pc) { do something } else do this }
=========================== 2nd way:
if( (data =inst.readLine()) != null ) // reading a line from txt file { String[] st = data.split("\\t"); // split using tab as a delimiter
String st_temp = Integer.toString(pc); // want to compare integer in string st[1] with pc which is an integer
if( st_temp.equals(st[1]) == true) { do something } else do this }
It doesn't work in both cases as the integer that I get from st[1] seems to be in a different format/encoding. the length of st[1] which contains "596" should come out to be 3 but comes out to be 7. If I do a substring extraction like st[1].substring(0,2) I get "5". Why is that happening?? However, the string I get when I convert pc= 596 comes out to be string of lenght 3.
Please advise. AL
JohnT - 19 Mar 2007 23:30 GMT On Mon, 19 Mar 2007 15:05:50 -0700, ashish.lohra wrote:
Can I make a small suggestion and it has nothing to do with your code. Why don't you start with a program that uses a String object set to some value like "596" and make sure that your code to convert it to integer is working properly. Then take your piece that reads from the file and have it read one line at a time, and echo back to you what it reads... convert it to hex or octal or something to make sure that you don't have any non-printable characters or anything. Then, when you know that you have both parts working the way you need them to, take your first section and convert it to a method which takes 1 argument, the String you want to convert. And have this method called for every String you want to convert.
JohnT - 19 Mar 2007 23:48 GMT On Mon, 19 Mar 2007 15:05:50 -0700, ashish.lohra wrote:
> I basically want ot compare an integer in string st[1] with another > integer in pc. [quoted text clipped - 47 lines] > Please advise. > AL I might get slapped for doing this, but here's a clue
public class ConvertStringToDecimal { public static void main (String [] args) { String s = "569"; ##### i = Convert(s); System.out.println("Length of " + s + " is " + s.length()); System.out.println("String converted to: " + i); } public static int Convert(String s) { return ######.parseInt(s); } }
fill in the blanks "#####'... they are both the same word.
Here's my output
Length of 569 is 3 String converted to: 569
Luc The Perverse - 20 Mar 2007 01:03 GMT *snip*
> I might get slapped for doing this, but here's a clue A clue? I must have missed something - why can't you just give him the answer? It took more work to remove something than to just leave it.
-- LTP
:) printdude1968@gmail.com - 20 Mar 2007 02:38 GMT On Mar 19, 8:03 pm, "Luc The Perverse" <sll_noSpamlicious_z_XX...@cc.usu.edu> wrote:
> *snip* > [quoted text clipped - 7 lines] > > :) All I did was give the code less one word to do the conversion. I don't know how to do the file business yet. I think he's doing the String to Integer part correctly, but there could be something wrong with his data file.. like a hidden CR/LF at the end or something..again not something that I know how to check for yet. Plus, it was a good exercise for me... took me about 1/2 hr to write the code and get it working... say.. is there a way to tell the length of an Integer? The only thing I could find was SIZE and that seems to report the number of bytes, not the length?
Patricia Shanahan - 19 Mar 2007 22:58 GMT > Hello, > [quoted text clipped - 11 lines] > > AL Most likely, the String is not really "596". It is some other value, 7 characters long, not the right format for parseInt.
Are you displaying the String at the point where you attempt conversion and seeing "596"? How are you reading it? Why are you sure it is "596"?
Patricia
ashish.lohra@gmail.com - 19 Mar 2007 23:10 GMT I display the string before trying to convert it and so I know it is "596". I am reading from a file using readLine() of BufferedReader and then split the string that I get from the readLine() method. One of the parts of the string that I get is what I am trying to compare. See my other message for my code.
I know it is somehting 7 chars long but then how do I make the comparison. Is there a way to convert it into integer or to create an stting with similar format from an integer?
Thanks a lot in advance. AL
Thomas Fritsch - 19 Mar 2007 23:19 GMT > I'm am working on converting a String "596" (I'm reading it from a txt > file) to an integer using parseInt() however I get [quoted text clipped - 7 lines] > > Please advise. If you have an IDE, start your application within the debugger of the IDE. Set a breakpoint before the line where you parse the string, and look inside your String.
If you don't have an IDE, dump out the String before you parse it: String s = ...; // your string for (int = 0; i < s.length(); i++) { char c = s.charAt(i); System.out.println("s[" + i + "] = " + (int)c + " = " + c); } See what output you get.
 Signature Thomas
ashish.lohra@gmail.com - 19 Mar 2007 23:53 GMT On Mar 19, 6:19 pm, Thomas Fritsch <i.dont.like.s...@invalid.com> wrote:
> ashish.lo...@gmail.com wrote: > > I'm am working on converting a String "596" (I'm reading it from a txt [quoted text clipped - 20 lines] > } > See what output you get. I used the above code segment and realized that before every integer there was a null char so the output being: s[0] = 0 = s[1] = 53 = 5 s[2] = 0 = s[3] = 54 = 6 s[4] = 0 = s[5] = 57 = 9 s[6] = 0 =
So i just created a new string using the chars I extracted from the above code.
Thanks for your help.
AL
> -- > Thomas Patricia Shanahan - 20 Mar 2007 00:05 GMT > On Mar 19, 6:19 pm, Thomas Fritsch <i.dont.like.s...@invalid.com> > wrote: ...
>> If you don't have an IDE, dump out the String before you parse it: >> String s = ...; // your string [quoted text clipped - 14 lines] > s[5] = 57 = 9 > s[6] = 0 = There may be a problem with how you are opening and reading the file. You would get what you are seeing if you took a file that was already in 16 bit character form, and opened it as though it were an 8-bit byte stream. The high order bits of each character, zero for tab and the digits, would turn into zero characters.
Patricia
Daniel Pitts - 20 Mar 2007 00:09 GMT On Mar 19, 3:53 pm, ashish.lo...@gmail.com wrote:
> On Mar 19, 6:19 pm, Thomas Fritsch <i.dont.like.s...@invalid.com> > wrote: [quoted text clipped - 44 lines] > > -- > > Thomas Sounds like you're not setting up the proper encoding for reading the string. While your "fix" may be working now, you should use the proper decoding by creating an InputStreamReader object, and passing THAT to your BufferedReader object.
Patricia Shanahan - 20 Mar 2007 02:54 GMT > On Mar 19, 3:53 pm, ashish.lo...@gmail.com wrote: ...
>> I used the above code segment and realized that before every integer >> there was a null char [quoted text clipped - 6 lines] >> s[5] = 57 = 9 >> s[6] = 0 = ...
> Sounds like you're not setting up the proper encoding for reading the > string. While your "fix" may be working now, you should use the > proper decoding by creating an InputStreamReader object, and passing > THAT to your BufferedReader object. I reached the same general conclusion, but in the opposite direction, superfluous decoding rather than missing decoding.
Suppose the file contained 569 as bytes, tab delimited, hex 09 35 36 39 09. Reading it as chars, omitting the byte to char conversion, would get a char containing hex 3536 or 3639, which we don't see.
Now suppose the file contains char data, hex 0009 0035 0036 0039 0009, and was read as a byte stream and passed through a byte to char decode, such as InputStreamReader.
The result would be hex 0000 0009 0000 0035 0000 0036 0000 0039 0000 0009. There are now seven characters between the two instances of horizontal tab, 0009, and those seven characters match the printout.
Patricia
Eric Sosman - 20 Mar 2007 01:51 GMT > Hello, > [quoted text clipped - 7 lines] > from the file and the length comes out to be 7. How did that happen? I > thought the length should be 3. The disagreement in length proves -- I said *proves* -- that the String you are trying to convert is something other than "596". I suggest you try to find out what it actually is.
 Signature Eric Sosman esosman@acm-dot-org.invalid
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|