4 Replies Latest reply on Dec 21, 2009 9:04 PM by 796440

    Need help converting string to int, NumberFormatException

    807580
      time is an integer array and outputLine is a String containing numbers. I keep getting a NumberFormatException error from the following line:

      time[index-1]=Integer.parseInt(outputLine.substring(0,13));

      How can I convert that substring into an integer and store it in the time[] array slot?

      Edited by: User12321 on Dec 21, 2009 12:21 PM
        • 1. Re: Need help converting string to int, NumberFormatException
          796440
          Show actual code, an SSCCE, and paste in the exact, complete error message.
          • 2. Re: Need help converting string to int, NumberFormatException
            796440
            User12321 wrote:
            time is an integer array and outputLine is a String containing numbers. I keep getting a NumberFormatException error from the following line:

            time[index-1]=Integer.parseInt(outputLine.substring(0,13));
            Then the first 13 characters of outputLine do not make a valid int. This should not be surprising, since the most number of characters in a valid Java int is 11, as can be seen by
            String.valueOf(Integer.MIN_VALUE).length()
            How can I convert that substring into an integer and store it in the time[] array slot?
            Sounds like you may need a long, rather than an int, but of course long has limits too. Or maybe there are just garbage characters in that string that don't belong in the int. Hard to say without more information from you.
            • 3. Re: Need help converting string to int, NumberFormatException
              796447
              jverd wrote:
              User12321 wrote:
              time is an integer array and outputLine is a String containing numbers. I keep getting a NumberFormatException error from the following line:

              time[index-1]=Integer.parseInt(outputLine.substring(0,13));
              Then the first 13 characters of outputLine do not make a valid int. This should not be surprising, since the most number of characters in a valid Java int is 11, as can be seen by
              String.valueOf(Integer.MIN_VALUE).length()
              Unless the value's numeric part (after the possible sign character) starts with some zeros, like:
              -000000001234
              Then it should still be ok. I of course agree the OP needs to provide more information.
              The ball is back in your court, OP.
              • 4. Re: Need help converting string to int, NumberFormatException
                796440
                warnerja wrote:
                jverd wrote:
                User12321 wrote:
                time is an integer array and outputLine is a String containing numbers. I keep getting a NumberFormatException error from the following line:

                time[index-1]=Integer.parseInt(outputLine.substring(0,13));
                Then the first 13 characters of outputLine do not make a valid int. This should not be surprising, since the most number of characters in a valid Java int is 11, as can be seen by
                String.valueOf(Integer.MIN_VALUE).length()
                Unless the value's numeric part (after the possible sign character) starts with some zeros, like:
                -000000001234
                Then it should still be ok.
                Good point.
                I of course agree the OP needs to provide more information.
                The ball is back in your court, OP.
                Yup.