我想将 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());
}
}
}
它不显示结果