Java String to integer conversion not working

A

ashish.lohra

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
 
J

JohnT

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

Can you post your code? What have you tried? Did you do a google search?
 
P

Patricia Shanahan

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

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
 
A

ashish.lohra

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
 
A

ashish.lohra

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
 
T

Thomas Fritsch

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

JohnT

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

JohnT

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

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
 
A

ashish.lohra

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.

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.

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
 
P

Patricia Shanahan

On Mar 19, 6:19 pm, Thomas Fritsch <[email protected]>
wrote: ....
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.

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 =

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
 
D

Daniel Pitts

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.

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

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

Luc The Perverse

*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.
 
E

Eric Sosman

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.

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

printdude1968

*snip*


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.

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?
 
P

Patricia Shanahan

Daniel said:
On Mar 19, 3:53 pm, (e-mail address removed) wrote: ....
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 =
....

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top