1

I'll admit it, I'm stumped. It's not a double. It's not outside of the range of an integer. It's not NAN. It's not a non-integer in any way shape or form as far as I can tell.

Why would I get that error?

Here's the code that causes it:

String filename = "confA.txt";

//Make a new filereader to read in confA
FileReader fileReader = new FileReader(filename);

//Wrap into a bufferedReader for sanity's sake
BufferedReader bufferedReader = new BufferedReader(fileReader);

//Get the port number that B is listening to
int portNum = Integer.parseInt(bufferedReader.readLine());

It fails on that last line, stating:

java.lang.NumberFormatException: For input string: "5000"

Which is the number I want.

I've also attempted

Integer portNum = Integer.parseInt(bufferedReader.readLine());

But that didn't work either. Neither did valueOf().

4
  • 9
    Try printing Arrays.toString(line.toCharArray()) (where line is what you read from the reader). You might have non-printable characters in the string. Commented Oct 8, 2017 at 19:01
  • 1
    @AndyTurner This appears to be it...somehow. [, 5, 0, 0, 0] was printed. Commented Oct 8, 2017 at 19:03
  • Thank you for your help, it's greatly appreciated. It looks like somehow the original text file just had something wrong with it, even if I couldn't see it. I re-wrote it and it works great now. :) Commented Oct 8, 2017 at 19:09
  • No problem. It's a simple debugging trick which helps surprisingly often. Commented Oct 8, 2017 at 19:15

2 Answers 2

3

Most probably there is some unprintable character somewhere in your file line. Please consider the following example (this was tested in Java 9 jshell)

jshell> String value = "5000\u0007";
value ==> "5000\007"

jshell> Integer.parseInt(value);
|  java.lang.NumberFormatException thrown: For input string: "5000"
|        at NumberFormatException.forInputString (NumberFormatException.java:65)
|        at Integer.parseInt (Integer.java:652)
|        at Integer.parseInt (Integer.java:770)
|        at (#15:1)

Here the string contains the "bell" character at the end. It makes parse to fail while it is not printed in exception text. I think you have something similar. The simpliest way to verify this is to check

String line = bufferedReader.readLine();
System.out.println("line length: " + line.length());

The value other than 4 will support my idea.

Sign up to request clarification or add additional context in comments.

Comments

0

I had the same problem. I was reading in a flat file with the Buffered reader and saving the contents as an ArrayList of type String, but on performing an integer.parse when retrieving a string value from the list, I realised that there was a whole lot of garbage in the string read from the file, as I got a java.lang.NumberFormatException.

This was the method I implemented with the code (called from my main method) to solve the problem:

` // class level

    private static final Pattern numericPattern = Pattern.compile("([0-9]+).([\\\\.]{0,1}[0-9]*)");

    // in main method after reading in the file
    String b = stripNonNumeric(stringvaluefromfile);
    int a = Integer.parseInt(b);

    public static String stripNonNumeric(String number) {

    //System.out.println(number);

    if (number == null || number.isEmpty()) {
        return "0";
    }

    Matcher matcher = numericPattern.matcher(number);

    // strip out all non-numerics
    StringBuffer sb = new StringBuffer("");
    while (matcher.find()) {
        sb.append(matcher.group());
    }

    // make sure there's only one dot
    int prevDot = -1;
    for (int i = sb.length() - 1; i >= 0; i--) {
        if (sb.charAt(i) == '.') {
            if (prevDot > 0) {
                sb.deleteCharAt(prevDot);
            }
            prevDot = i;
        }
    }

    if (sb.length() == 0) {
        sb.append("0");
    }

    return sb.toString();
}`

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.