私はJavaが新しく、.txtファイルをスキャンして個々の単語を取得しようとしました。テキストファイルのスキャナーがArrayListに要素を追加できません
ArrayListに要素が追加されていないため、どこが間違っていましたか?
public class ScanInputFile {
public static ArrayList<String> list = new ArrayList<String>();
public static void importArrayList(String path) throws FileNotFoundException {
File textFile = new File(path);
Scanner s = new Scanner(textFile);
while (s.hasNext()){
list.add(s.next());
}
s.close();
}
public static void main(String[] args) throws FileNotFoundException {
importArrayList("C:\\MyFirstList.txt");
System.out.println(list.get(0));
}
}
これはtxtファイルは、行ごとに2つのワード10語を含んでいてもIndexOutOfBounds例外をスロー。
FileNotFoundExceptionsに問題があったため、path.exists()とpath.canRead()がtrueに戻っていることを確認しました。
ありがとうございます!
'hasNext()'が少なくとも真であるかどうかデバッグしましたか? – Smutje
完全なエラーを表示 –
はい、私はしました。以下のようにtxtファイルを置き換えるまで、それは決してtrueを返しませんでした。 – Sp33dyGonzal3s