我试图从列出的文件,其中包含一个陈述文字打印一行。但该计划什么都不做。有人可以帮助我的代码?由于程序终止,不打印任何东西
import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;
public class SearchingArrayLists {
public static void main(String[] args) throws Exception {
ArrayList names = new ArrayList();
Scanner scan = new Scanner(new File("random.txt"));
while (scan.hasNext()){
names.add(scan.next());
}
if (names.contains("legal")){
System.out.println(scan.next());
}
scan.close();
}
}
UPDATE:
对不起,去除循环。该文件包含随机文本,其中有“合法”一词。该文件被扫描仪事先读取。
为什么空的'for'循环? – Maroun 2014-12-09 13:35:24
'random.txt'包含什么? 并且该文件正在被扫描仪读取? – 2014-12-09 13:35:42
是否random.txt含有法律也不会有下一个()这里所说的:'的System.out.println(scan.next());'因为你已经在整个文件中重复。 – brso05 2014-12-09 13:36:28