NumberFormatException given an input String holdin

2019-06-19 21:59发布

问题:

I have a String from which I would like to parse an integer and cannot find a way past this runtime exception. I understand that it is meant to display at times when a parseNUMBERTYPE function is applied to an inappropriately defined String, and that blank spaces or letters where the code expects numbers to be can trigger it. However, the String I am using as a test dummy is as far as I can tell simply the numeral 5. I have seen several suggestions in response to other users' NumberFormatException problems advocating the application of a trim() function before parsing, and I have tried this with no success.

I have also tried replacing the String I wish to parse with the simple, unstored value "5". This is the same as what the program seems to report as the relevant variable's stored String value, but while parsing that variable fails with this post's eponymous exception, the unstored value appears to run perfectly well in its place.

Note that the String variable is read by a File Scanner. I must suppose my problem has something to do with unknown, unwanted, 'invisible' characters that are being read in addition to the number five, but I cannot determine why this is happening or how to stop it. The file is formatted as .txt

Here is my code:

    Scanner scan = new Scanner(System.in);
    System.out.println("Please enter the file name:");
    String filename = scan.nextLine();      
    File tempfile = new File(filename);
    Scanner filereader = new Scanner(tempfile);

    //this is meant to assign to the String line1 the numerical value (5)
    //of the file's first line
    String line1 = filereader.nextLine();

    //this was added later to determine if line1 held the value I expect
    //it outputs the single digit 5, as expected and appropriate
    System.out.println(line1);

    //this was added to test how flawed my system was
    //it runs fine, indicating to me that the problem may in my reader
    int num2 = Integer.parseInt("5");

    //this is where the exception is cast
    int num1 = Integer.parseInt(line1);

I am presented with these errors:

Exception in thread "main" java.lang.NumberFormatException: For input string: "5"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:492)
at java.lang.Integer.parseInt(Integer.java:527)
at Driver.main(Driver.java:26)

Your assistance is appreciated.

In response to the suggestions given so far, the code has been modified to appear as follows:

    Scanner scan = new Scanner(System.in);
    System.out.println("Please enter the file name:");
    String filename=scan.nextLine();        
    File tempfile = new File(filename);
    Scanner filereader = new Scanner(tempfile);

    //this is meant to assign the String line1 the numerical value (5)
    //of the file's first line
    String line1 = filereader.nextLine();

    //this was added after to determine if line1 held the value I expect
    //it outputs the single digit 5, as expected and appropriate
    System.out.println("["+line1+"]");

    //this was added to test how flawed my system was
    //it runs fine, indicating to me that the problem is in my reader
    int num2 = Integer.parseInt("5");

    //this is where the exception is cast
    int num1 = Integer.parseInt(line1.trim());

Please correct me if I have misinterpreted your guidance in some way. As it stands, the problem persists, with an identical error report.

回答1:

Looking at the message in the exception, it shows that there are no additional visible characters or whitespace in the string since the 5 is surrounded by quotes (and a quick check of the Java source code shows that the message printed here does not appear to be modified to remove whitespace before surrounding the string with quotes).

This means that either there are hidden non-printing characters in your string, or the 5 itself is actually not a 5 at all, and is instead some unicode character from another language that resembles a 5.

Simple debug case to sort this out would be to print the length of your string, as this will quickly sort out whether there are additional hidden characters in it.

System.out.println("String: [" + line1 + "] size: " + line1.size());

After that, a regex can be used to get the first consecutive set of digits, if that is the desired behaviour:

    Pattern pattern = Pattern.compile("(\\d+)");
    Matcher matcher = pattern.matcher(line1);
    if (matcher.find())
    {
        String digits = matcher.group();
        int num = Integer.parseInt(digits);
    }

Alternatively, if it is possible or desired to remove characters in between digits then a replaceAll can be used:

 String digits = line1.replaceAll("[^0-9]","");

For the string "5 6", the first method will give you 5, and the second will give you 56.