【发布时间】:2016-01-28 12:27:03
【问题描述】:
我正忙于编写一个算法,它将扫描一个文本文件并将其内容添加到一个数组中。但是我不断收到一条错误消息,上面写着:
“线程“AWT-EventQueue-0”中的异常 java.util.NoSuchElementException"
我已阅读 java 文档并了解到此错误与我没有正确设置我的文本文件的事实有关,但是我不明白如何修复它。
我已经包含了我的代码和文本文件的内容:
String [] CardPictureNames = new String [16];
try {
Scanner car = new Scanner (new File("cardNames.txt")).useDelimiter("#");
String CN = "";
while(car.hasNext()){
for(int j=0; j <= CardPictureNames.length; j++ ){
CN = car.next();
CardPictureNames[j] = CN;
}
}
car.close();
}
catch (FileNotFoundException ex) {
Logger.getLogger(MemoryForm.class.getName()).log(Level.SEVERE, null, ex);
JOptionPane.showMessageDialog(this,"The file containing the names of the cards is missing.");
}
以下是文本文件的内容:
2 of Clubs#
10 of Clubs#
Ace of Hearts#
Ace of Spades#
Joker#
King of Hearts#
Queen of Clubs#
Queen of Diamonds#
2 of Clubs#
10 of Clubs#
Ace of Hearts#
Ace of Spades#
Joker#
King of Hearts#
Queen of Clubs#
Queen of Diamonds#
有人可以向我解释一下如何解决这个问题吗? 提前谢谢你。
【问题讨论】: