我正在尝试做一个应用程序,应该有助于使地下城和龙的项目更容易为用户!
我有3个不同武器的列表,有不同的统计数据等。其中2个列表添加到程序中很好,但其中1个抛出了NoSuchElementException。我真的不明白为什么,因为我找不到任何与这个列表有任何不同的东西,与我得到的其他列表相比。
列表中的一行如下所示:
Light Melee Weapon,1d4,1d6,x2,10 ft.,Slashing,Axe (Throwing),8 gp, 2lb.http://pastebin.com/Fb2db0f1链接到整个问题列表,如果你想看看的话。
http://pastebin.com/9Hg0Rw2a还可以链接到一个运行良好的列表!
我希望这个方法对您来说不会太长。我真的已经做了我能想到的所有事情来尝试解决这个问题。我敢肯定是这个列表导致了问题,因为一旦我从它读取的目标中删除它,程序就会运行得很好!
它在可以工作的列表上运行for循环,但是第一个列表不能在._上工作。
Exception in thread "main" java.util.NoSuchElementException
at java.util.StringTokenizer.nextToken(StringTokenizer.java:349)
at MainGUI.init(MainGUI.java:60)
at MainGUI.main(MainGUI.java:32)
public HashMap<String,Weapon> init() {
String path = "base";
File folder = new File(path);
File[] listOfFiles = folder.listFiles();
if(listOfFiles.length == 0) {
JOptionPane.showMessageDialog(MainGUI.this, "The folder \"base\" was empty"+
" so no weapons or armor was attempted loaded.", "Warning",
JOptionPane.WARNING_MESSAGE);
return new HashMap<>();
}
HashMap<String, Weapon> weapons = new HashMap<>();
BufferedReader r = null;
for(File f : listOfFiles) {
if(f.getName().endsWith(".txt")) {
String line;
try {
r = new BufferedReader(new FileReader(f));
while((line = r.readLine()) != null) {
Weapon w;
StringTokenizer st = new StringTokenizer(line,",");
while(st.hasMoreTokens()) {
w = new Weapon(WeaponCategory.fromString(st.nextToken()),
st.nextToken(),st.nextToken(),st.nextToken(),
st.nextToken(),st.nextToken(),st.nextToken(),
st.nextToken(),st.nextToken());
weapons.put(w.getName(), w);
}
}
} catch(FileNotFoundException fnfe) {
JOptionPane.showMessageDialog(MainGUI.this, "The File "+
f.getName() + " was not found.", "Error",
JOptionPane.ERROR_MESSAGE);
} catch(IOException ioe) {
JOptionPane.showMessageDialog(MainGUI.this, "There was a problem reading "+
f.getName() + ".", "Error",
JOptionPane.ERROR_MESSAGE);
}
}
}
try {
r.close();
} catch (IOException ex) {
JOptionPane.showMessageDialog(MainGUI.this, "An error occured while"
+ "closing the File Reader Stream:\n"+ex.getMessage(),
"Error", JOptionPane.ERROR_MESSAGE);
}
return weapons;
}发布于 2012-06-22 00:29:30
像这样的代码
while(st.hasMoreTokens()) {
w = new Weapon(WeaponCategory.fromString(st.nextToken()),
st.nextToken(),st.nextToken(),st.nextToken(),
st.nextToken(),st.nextToken(),st.nextToken(),
st.nextToken(),st.nextToken());
weapons.put(w.getName(), w);
}是不安全的,因为您假设每一个输入行都有9个项目。你应该检查项目的数量,并优雅地处理无效的行(例如,写出行的内容,以便更容易地识别问题)。
要实现这一点,一种简单的方法是使用String.split()对行进行标记。它返回一个String[],您可以在尝试访问它的元素之前轻松地检查它的大小。
发布于 2012-06-22 00:31:33
Stringtokenizer现在有点过时了。
如果您确定不会有包含逗号的条目,则可以使用Scanner或仅使用逗号拆分。
如果您使用了split,则可以检查部件中的条目数量是否包含正确数量的元素:
String[] parts = line.split(",")
if (parts.length != 9){
throw new RuntimeException("Bad line : " + line);
}
...
//continue processing 您还可以让计数器在每一行上递增,然后您可以显示导致异常的行。
发布于 2012-06-22 00:41:49
如果输入耗尽了,就会发生NoSuchElementException,这在标准输入中是不太可能发生的(相反,它会阻塞)。不正确的值将产生InputMismatchException。
如果没有任何数据类型检查,那么不用担心。但是如果你想有不同的数据类型,那么在添加到bean对象之前应该检查一下。
https://stackoverflow.com/questions/11142516
复制相似问题