大家好我有這樣的代碼:不應該出現的奇怪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());
正如你所看到的,我已經做了一個整數的值,但我仍然得到它說它的一個字符串,當我嘗試輸出它,謝謝。
我也不例外。但是你有一個無限循環。線路不會再次設置。 – pL4Gu33
'ItemIdReader'不在堆棧跟蹤中,問題在其他地方...並且你有一個無限循環,如大家所說 –
你有無限循環... ..你有Infiniteloop .... ......不,你有無限的lo .... .... !!!! 「Question is about NumberFormatException」 Nooo ...你有無限循環.... :) –