NumberFormatException给定一个包含小整数的输入String

时间:2013-09-27 18:30:55

标签: java string file parsing numberformatexception

我有一个String,我想从中解析一个整数,并且找不到超出此运行时异常的方法。我理解它有时会显示一个parseNUMBERTYPE函数应用于一个不恰当定义的String,并且代码期望数字的空格或字母可以触发它。但是,我用作测试虚拟的字符串就我简单地说数字5.我已经看到了几个建议,以响应其他用户的NumberFormatException问题,主张在解析之前应用trim()函数,我试过这个没有成功。

我还尝试用简单的,未存储的值“5”替换我想要解析的String。这与程序似乎报告为相关变量的存储字符串值相同,但在解析该变量时,由于此帖子的同名异常,未存储的值似乎在其位置上运行得非常好。

请注意,String变量由文件扫描程序读取。我必须假设我的问题与未知的,不需要的,“不可见的”字符有关,这些字符除了第五个之外还被读取,但我无法确定为什么会发生这种情况或如何阻止它。该文件格式为.txt

这是我的代码:

    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);

我遇到了这些错误:

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)

感谢您的协助。

对于到目前为止给出的建议,代码已被修改为如下所示:

    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());

如果我以某种方式误解了你的指导,请纠正我。目前看,问题仍然存在,并有相同的错误报告。

1 个答案:

答案 0 :(得分: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

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

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