-1

我想将 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());
          }
    }


}

它不显示结果

4

1 回答 1

7

你要

   if (grand[j].equals(query)){
       System.out.println (grand[j]);
   }

即使用索引j,因为这就是您正在循环的内容。

正如评论者所指出的,您应该调查 IDE 的调试器。

于 2013-05-29T10:18:06.203 回答