我想将txt文件中的整行存储到我设法做到的数组中但是当从数组中搜索时我似乎无法做到这一点。这就是代码:
import java.io.*;
import java.util.*;
public class GrandFinal {
public void readFromFile()throws IOException {
String[] grand = new String[200];
Scanner search = new Scanner(System.in);
String query;
try
{
Scanner reader = new Scanner(new FileInputStream("NRLdata.txt"));
int i = 0;
while (reader.hasNext()){
i++;
grand[i] = reader.next();
}
System.out.println("Search for GrandFinal: ");
query = search.next();
for(int j = 0; j <grand.length; j++)
{
if(grand[i].equals(query)){
System.out.println (grand[j]);
}
}
reader.close();
}catch (FileNotFoundException e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
}
它不显示结果
答案 0 :(得分:7)
你想要
if (grand[j].equals(query)){
System.out.println (grand[j]);
}
即。使用索引 j ,因为这就是你要循环的内容。
正如评论者所说,您应该调查IDE的调试器。