[英]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.