Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Java String to integer conversion not working

Reply
Thread Tools

Java String to integer conversion not working

 
 
ashish.lohra@gmail.com
Guest
Posts: n/a
 
      03-19-2007
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

 
Reply With Quote
 
 
 
 
JohnT
Guest
Posts: n/a
 
      03-19-2007
On Mon, 19 Mar 2007 14:45:28 -0700, ashish.lohra wrote:

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


 
Reply With Quote
 
 
 
 
Patricia Shanahan
Guest
Posts: n/a
 
      03-19-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> 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
 
Reply With Quote
 
ashish.lohra@gmail.com
Guest
Posts: n/a
 
      03-19-2007
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

 
Reply With Quote
 
ashish.lohra@gmail.com
Guest
Posts: n/a
 
      03-19-2007

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

 
Reply With Quote
 
Thomas Fritsch
Guest
Posts: n/a
 
      03-19-2007
(E-Mail Removed) wrote:
> 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.

--
Thomas
 
Reply With Quote
 
JohnT
Guest
Posts: n/a
 
      03-19-2007
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.


 
Reply With Quote
 
JohnT
Guest
Posts: n/a
 
      03-19-2007
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.
> 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

 
Reply With Quote
 
ashish.lohra@gmail.com
Guest
Posts: n/a
 
      03-19-2007
On Mar 19, 6:19 pm, Thomas Fritsch <(E-Mail Removed)>
wrote:
> (E-Mail Removed) wrote:
> > 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
> --
> Thomas



 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      03-19-2007
(E-Mail Removed) wrote:
> On Mar 19, 6:19 pm, Thomas Fritsch <(E-Mail Removed)>
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
is there a way to AutoParse a string to another type - e.g. if aDate format then date, else if integer than Integer etc ????? Greg Hauptmann Ruby 6 08-06-2008 04:52 PM
Change a string to an integer, report an error if the string does not represent an integer? Randy Kramer Ruby 12 10-25-2007 09:56 PM
how do I make Class.forName("Integer") returning java.lang.Integer? Johannes Zellner Java 22 12-19-2005 11:22 AM
conversion of signed integer to unsigned integer junky_fellow@yahoo.co.in C Programming 14 06-18-2005 02:29 PM
CRA Editor Integer to String Conversion Phil Cisco 0 07-30-2004 06:10 PM



Advertisments