Обычно мы не используем Scanner
Object для чтения файла, так как это не Best Practice
проверьте этот пример как указал @Zabuzard.
Решение :
Scanner sf = new Scanner(new File("amazing.txt"));
List < String > text = new ArrayList < > ();
while (sf.hasNextLine()) {
String current = sf.nextLine();
// if (current.endsWith(",") || current.endsWith(";") || current.endsWith("!"))
// System.out.println(current);
String all_words[];
all_words = current.split(" "); //create an array with all strings seperated with space for each line
System.out.print("All words of the line:");
for (int i = 0; i < all_words.length; i++) {
System.out.print(all_words[i] + " ");
//after you do in this section your checks, add it to the List
}
System.out.println();
}
sf.close();
Это напечатает все слова каждой строки. Вы можете продолжить реализацию своего логина в вашем сценарии и trim
строк, чтобы удалить специальные символы.
Затем вы можете продолжить, добавив его в свой List<String>
.