奇怪的NumberFormatException不应该出现?

时间:2014-05-10 09:52:39

标签: java numberformatexception

大家好,我有这个代码:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class ItemIdReader {
    public int id;
    public ItemIdReader(){

        try {
            BufferedReader br = new BufferedReader(new FileReader("itemList.txt"));
            String line = br.readLine();
            while (true) {
                if (line == null)
                    break;

                String[] split = line.split(" - ", 2);

                String itemName = split[1];
                id= Integer.parseInt(split[0]);
            }
        } catch (IOException i ) {

            i.printStackTrace();
        }
    }

    public int getId() {
        return id;
    }
}

当我尝试输出id时,我收到此错误:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "1"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at ItemIdReader.<init>(ItemIdReader.java:19)
at Launcher$UI.lambda$new$0(Launcher.java:25)
at Launcher$UI$$Lambda$1/455659002.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:744)
at java.awt.EventQueue.access$400(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.awt.EventQueue$3.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:714)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

这是.txt文件,我可以阅读:

1 - Toolkit

这是println:

            ItemIdReader newF= new ItemIdReader();
            System.out.println(newF.getId());

正如你所看到的,我已经将值id设为一个整数但是当我尝试输出它时,我仍然得到它的字符串,谢谢。

4 个答案:

答案 0 :(得分:2)

首先,如果你没有这个例外,你就会有一个无限循环。

我的猜测是你在文件的开头有一个隐藏的角色,可能是一个BOM。通过将每个字符串转换为整数来确认它,如果已经确认,则使用编辑器重新打开文件,并确保不使用BOM保存它。

for (char c : split[0].toCharArray()) {
    System.out.println((int) c); 
}

答案 1 :(得分:0)

如果你在循环中移动readln它可以正常工作

while (true) {        
String line = br.readLine();

            if (line == null)
                break;

            String[] split = line.split(" - ", 2);

            String itemName = split[1];
            id= Integer.parseInt(split[0]);
        }

答案 2 :(得分:0)

这是一个无限循环! 你应该在里面再读一行。此代码与您想要的类似,它适用于我:

public class Test {
    public static void main(String[] a) throws IOException{
       BufferedReader br = new BufferedReader(new FileReader("itemList.txt"));
       String line = br.readLine();
        while (true) {
            if (line == null)
                break;
            String[] split = line.split(" - ", 2);
           String itemName = split[1];
           int id= Integer.parseInt(split[0]);
           System.out.println(id);
           line = br.readLine();
        }
    }
}

答案 3 :(得分:0)

JB Nizet是对的。确实存在字节顺序标记,它会被复制到您的错误消息中。

您可以使用BOMInputStream读取您的文件,以避免这种情况。