-1 java arrays try-catch fileinputstream
我想将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());
}
}
}
Run Code Online (Sandbox Code Playgroud)
它不显示结果
你要
if (grand[j].equals(query)){
System.out.println (grand[j]);
}
Run Code Online (Sandbox Code Playgroud)
即使用索引j,因为这就是你正在循环的东西.
正如评论者所指出的那样,您应该调查IDE的调试器.
归档时间: |
|
查看次数: |
107 次 |
最近记录: |