6

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.

4

1 回答 1

1

查看异常中的消息,它表明字符串中没有额外的可见字符或空格,因为 5 被引号包围(并且快速检查 Java 源代码表明此处打印的消息似乎不是修改为在用引号括起字符串之前删除空格)。

这意味着您的字符串中有隐藏的非打印字符,或者 5 本身实际上根本不是 5,而是来自另一种语言的类似于 5 的一些 unicode 字符。

解决这个问题的简单调试案例是打印字符串的长度,因为这将快速找出其中是否有其他隐藏字符。

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

之后,可以使用正则表达式来获取第一组连续的数字,如果这是所需的行为:

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

或者,如果可以或希望删除数字之间的字符,则replaceAll可以使用 a:

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

对于字符串“5 6”,第一种方法会给你 5,第二种方法会给你 56。

于 2013-09-27T19:21:45.863 回答