Java String to integer conversion not working

Discussion in 'Java' started by ashish.lohra@gmail.com, Mar 19, 2007.

  1. Guest

    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
     
    , Mar 19, 2007
    #1
    1. Advertising

  2. JohnT Guest

    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?
     
    JohnT, Mar 19, 2007
    #2
    1. Advertising

  3. 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
     
    Patricia Shanahan, Mar 19, 2007
    #3
  4. Guest

    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
     
    , Mar 19, 2007
    #4
  5. Guest

    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
     
    , Mar 19, 2007
    #5
  6. 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
     
    Thomas Fritsch, Mar 19, 2007
    #6
  7. JohnT Guest

    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, Mar 19, 2007
    #7
  8. JohnT Guest

    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
     
    JohnT, Mar 19, 2007
    #8
  9. Guest

    On Mar 19, 6:19 pm, Thomas Fritsch <>
    wrote:
    > 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
     
    , Mar 19, 2007
    #9
  10. wrote:
    > On Mar 19, 6:19 pm, Thomas Fritsch <>
    > 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
     
    Patricia Shanahan, Mar 19, 2007
    #10
  11. Daniel Pitts Guest

    On Mar 19, 3:53 pm, wrote:
    > On Mar 19, 6:19 pm, Thomas Fritsch <>
    > wrote:
    >
    >
    >
    > > 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


    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.
     
    Daniel Pitts, Mar 19, 2007
    #11
  12. "JohnT" <> wrote in message
    news:p...
    *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

    :)
     
    Luc The Perverse, Mar 20, 2007
    #12
  13. Eric Sosman Guest

    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.


    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.

    --
    Eric Sosman
    lid
     
    Eric Sosman, Mar 20, 2007
    #13
  14. Guest

    On Mar 19, 8:03 pm, "Luc The Perverse"
    <> wrote:
    > "JohnT" <> wrote in message
    >
    > news:p...
    > *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
    >
    > :)


    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?
     
    , Mar 20, 2007
    #14
  15. Daniel Pitts wrote:
    > On Mar 19, 3:53 pm, 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
     
    Patricia Shanahan, Mar 20, 2007
    #15
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Johannes Zellner
    Replies:
    22
    Views:
    3,614
  2. syntax
    Replies:
    1
    Views:
    393
    Aggro
    Jan 25, 2004
  3. sam
    Replies:
    13
    Views:
    23,495
    Chris Croughton
    Apr 19, 2005
  4. Replies:
    14
    Views:
    2,117
    CBFalconer
    Jun 18, 2005
  5. Randy Kramer
    Replies:
    12
    Views:
    377
    Robert Klemme
    Oct 25, 2007
Loading...

Share This Page