2013-04-21 63 views
2
public void loadFromFile(String filename) { 
    File file = new File(filename); 
    BufferedReader br; 

    try { 
     br = new BufferedReader(new FileReader(file)); 
     numberOfAttributes = Integer.parseInt(br.readLine()); 
    } 
    ... 
} 

上面是我的程序:我试图从txt文件中读取,其中第一行是数字22,没有更多。我不知道为什么这个程序给了我一个例外。java.lang.NumberFormatException:对于输入字符串:“22”

+1

什么是例外,我们可以看到异常的堆栈跟踪多一点代码。 – blackpanther 2013-04-21 19:12:41

回答

1

br.readLine()读取包括新线特殊character.Apart整条生产线,形成由詹姆斯提出的解决方案,你可以使用Scanner#nextInt()

+0

在该文档中读取返回值:“包含该行内容的字符串,**不包括任何行终止符**,或者如果已到达流末尾,则返回null” – 2013-04-21 19:17:25

+0

java.util.InputMismatchException,它给了我。 – ToBeGeek 2013-04-21 19:53:16

5

尝试从字符串剥离所有的空白:

 numberOfAttributes = Integer.parseInt(br.readLine().trim()); 
+3

或者只是使用'trim' – Alex 2013-04-21 19:15:54

+0

他们都不工作,trim()或上面。不知道为什么 – ToBeGeek 2013-04-21 19:20:23

+0

String a = br.readLine(); Integer.parseInt(a); System.out.println(a);我尝试过,仍然给我相同的例外。 – ToBeGeek 2013-04-21 19:22:55

0

尝试numberOfAttributes = Integer.parseInt(br.readLine().trim());

public String trim()

Returns a copy of the string, with leading and trailing whitespace omitted. If this String object represents an empty character sequence, or the first and last characters of character sequence represented by this String object both have codes greater than '\u0020' (the space character), then a reference to this String object is returned.

Otherwise, if there is no character with a code greater than '\u0020' in the string, then a new String object representing an empty string is created and returned.

3

我想你可能有一个UTF-8 BOM(字节顺序标记)在你的文件的开始。

下面是重现该错误的类:

import java.io.*; 

public class BomTest { 
    public static void main(String[] args) throws Exception { 
     File file = new File("example.txt"); 

     // Write out UTF-8 BOM, followed by the number 22 and a newline. 
     byte[] bs = { (byte)0xef, (byte)0xbb, (byte)0xbf, (byte)'2', (byte)'2', 10 }; 
     FileOutputStream fos = new FileOutputStream(file); 
     fos.write(bs); 
     fos.close(); 

     BufferedReader r = new BufferedReader(new FileReader(file)); 
     String s = r.readLine(); 
     System.out.println(Integer.parseInt(s)); 
    } 
} 

当我运行这个类,我得到下面的输出:

[email protected]:~$ java BomTest 
Exception in thread "main" java.lang.NumberFormatException: For input string: "22" 
     at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) 
     at java.lang.Integer.parseInt(Integer.java:481) 
     at java.lang.Integer.parseInt(Integer.java:514) 
     at BomTest.main(BomTest.java:15) 

是不是真的有一个简单的方法来处理UTF -8 Java中的BOM;最好不要生成它们。另请参阅this answer

+0

我和OP有完全相同的问题,但没有任何解决方案。因为这个,我一直在撕掉我的头发,并意识到这是因为你提到的确切问题。将我的笨文本文件保存为ANSI编码而不是UTF-8。希望我的2个小时后回来。 – Nopiforyou 2014-04-07 16:49:12

0

发生这种情况是因为输入行中有空格。看看这些:

int i1 = Integer.parseInt("22 "); 
int i2 = Integer.parseInt("22a"); 
int i3 = Integer.parseInt("2 2"); 
int i4 = Integer.parseInt("22\n"); 

所有这些都会产生异常。我建议你修剪,标记或替换。但总的来说,我觉得这不是一个很好的解决方案,以这种方式从文件中读取数字。 如果你真的需要存储数据,为什么不创建一个临时对象并序列化/反序列化它?

+0

不是。如果仔细观察每个示例引发的NumberFormatExceptions,您会发现它们与提问者给出的错误不匹配。 – 2013-04-21 20:50:37

0

您的字符串中可能有空字符。使用regEx“\ d +”删除它。

NumberFormatException是因为输入字符串不是预期的数字格式而引发的。通常,您可以在错误消息中看到“错误的字符串输入”,并且可以轻松识别错误。但在你的情况下,错误消息不会完全显示字符串输入(因为它不显示空字符)。

检查下面的输出和代码。

public class TestParseInt{ 
    private static final Pattern pattern = Pattern.compile("\\d+"); 
    public static void main(String []args){ 
     String a = "22\0"; 
     try { 
      System.out.println("Successfull parse a: " + Integer.parseInt(a)); 
     } catch(NumberFormatException e) { 
      System.out.println("Error:" +e.getMessage()); 
     } 
     try { 
      Matcher matcher = pattern.matcher(a); 
      if(matcher.find()) { 
       System.out.println("Succesfull parse a: " + 
       Integer.parseInt(matcher.group(0))); 
      } 

     } catch(NumberFormatException e) { 
      System.out.println("Error" + e.getMessage()); 
     } 

    } 
} 

输出:

错误:对于输入字符串: “22”
Succesfull解析:22

相关问题