[英]Program terminates with no error message
这是我不得不用于问题的代码。 我应该在Eclipse中的命令行中搜索单词,方法是键入Java Spell a,然后在从本地文本文件开始的控制台中输出所有单词。 我的程序无故终止:
import java.util.*;
import java.io.*;
public class Spell {
public static void main (String[] args) throws Exception {
File file = new File("C:\\Users\\Dennis\\words.txt");
Scanner fileScanner = new Scanner(file);
while (fileScanner.hasNext()) {
String word = fileScanner.next();
if(word.startsWith(args[0])) {
System.out.println(word);
}
}
}
}
如果您以java Spell garfield
形式运行此程序,则args[0]
将在文件中包含garfield(如果有任何单词与garfield匹配),则将执行您的print语句,否则将不执行。您必须在ecclipse
提供运行时参数ecclipse
运行您的程序,然后您将能够看到所需的实际输出。
右键单击您的Class
main method
,在tab (x)=Arguments
选择run as->run configuration
tab (x)=Arguments
提供您的参数,然后运行,下面是此屏幕截图
在eclipse中,右键单击Java文件->运行方式->运行配置->选项卡参数,在程序参数中输入a。 如果未输入任何参数,则应在该行处获得“ ArrayIndexOutOfBoundsException”
if(word.startsWith(args[0])) {
我建议在线上设置一个断点
String word = fileScanner.next();
然后调试。 这样您就可以验证程序扫描的单词,并检查arg [0]是否是您输入的内容。
我已经用word.txt运行您的程序,例如
asdf bsdf csdf dsdf
和参数c
,它打印出来
csdf正确地。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.