【问题标题】:Scanner not assigning values to array扫描仪未将值分配给数组
【发布时间】:2016-01-19 22:29:09
【问题描述】:
FileReader myReader = new FileReader(myReaderRef);
    Scanner input = new Scanner(myReader);
    int arraySize = 0;
    while(input.hasNextLine()){
        arraySize++;
        input.nextLine();
    }

    int[] numbers;
    numbers = new int[arraySize];

    while(input.hasNextLine()){
        for(int i=0; i < numbers.length; i++){
            numbers[i] = input.nextInt();
        }
    }
    input.close();
    System.out.print(numbers[1]);
}
}

它正在读取的文本文件如下所示:

10
2
5
1
7
4
9
3
6
8

每当我使用 system.out.print 输出其中一个数组插槽时,无论我调用哪个数组位置,它都只会给我 0。我哪里错了?

编辑:我不得不关闭并重新启动文件阅读器和扫描仪。感谢您的帮助!

【问题讨论】:

    标签: java arrays file-io io


    【解决方案1】:

    您需要在计算行数后重新启动您的Scanner(因为它位于File 的末尾)。

    Scanner input = new Scanner(myReader);
    int arraySize = 0;
    while(input.hasNextLine()){
        arraySize++;
        input.nextLine();
    }
    input.close(); // <-- close it.
    myReader = new FileReader(myReaderRef); // Create a new FileReader
    input = new Scanner(myReader); // <-- create another scanner instance
    

    【讨论】:

    • 只是这样做了,我仍然得到所有插槽的 0。
    • 如果你这样做input.close(),文件阅读器不会也被关闭吗?这意味着你也必须再次初始化它......
    【解决方案2】:

    您尝试通读该文件两次,而无需回到开头。第一次是数行数,第二次是读取数据。因为您不会回到文件的开头,所以不会在任何地方加载/存储数据。所以你应该重新启动你的Scanner,例如。关闭并重新打开。

    或者您可能想考虑使用ArrayList,这样您只需读取文件一次。

    List<Integer> numbers = new ArrayList<Integer>();
    Scanner input = new Scanner(myReader);
    while (input.hasNextLine()) {
        numbers.add(input.nextInt());
        input.nextLine();  // You might need this to get to the next line as nextInt() just reads the int token.
    }
    input.close()
    

    【讨论】:

    • 我对数组列表还不太熟悉,所以我只是在试验我目前所知道的。在我得到我目前使用的东西后,我可能会考虑尝试一下。感谢您的意见!
    【解决方案3】:

    在您的第一个 while 循环中,光标已经到了文件的最后一行。所以它在下一个循环中找不到任何东西。所以你必须创建 2 个Scanner 对象。

            Scanner input = new Scanner(new File("D:\\numbers.txt"));
            int arraySize =0;
            while(input.hasNextLine()){
                arraySize++;
                input.nextLine();
            }
    
            int[] numbers;
            numbers = new int[arraySize];
            input.close();
            Scanner input2 = new Scanner(new File("D:\\numbers.txt"));
            while(input2.hasNextLine()){
                for(int i=0; i < numbers.length; i++){
                    numbers[i] = input2.nextInt();
                }
            }
            input2.close();
            System.out.print(numbers[1]);
    

    【讨论】:

      【解决方案4】:

      我试图尽可能地简化问题。

      根据您的情况向 Array 添加/删除元素的最简单方法是使用 ArrayList 对象。通读 cmets 并运行项目。

      下面的第一个整数列表是文件的原始输入。

      第二个列表包含数组的打印语句。这些答案可能不是您期望它们被索引的地方,但我相信这会让您走上正确的道路:)

      10
      2
      5
      1
      7
      4
      9
      3
      6
      8
      

      [10, 2, 5, 1, 7, 4, 9, 3, 6, 8]
          3
          1
          7
          5
          2
          8
      
      
      
      
      
      
      
      package cs1410;
      
          import java.io.File;
          import java.io.FileNotFoundException;
          import java.util.ArrayList;
          import java.util.Scanner;
      
          import javax.swing.JFileChooser;
      
          public class ArrayReader {
      
              public static void main(String[] args) throws FileNotFoundException {
                  // Creates an array list
                  ArrayList<Integer> answer = new ArrayList<Integer>();
      
                  // Reads in information
                  JFileChooser chooser = new JFileChooser();
                  if (JFileChooser.APPROVE_OPTION != chooser.showOpenDialog(null)) {
                      return;
                  }
                  File file = chooser.getSelectedFile();
      
      
      
                  // Scan chosen document
                  Scanner s = new Scanner(file);
                  int count = 0;
      
      
                  // Scans each line and places the value into the array list
                  while (s.hasNextLine()) {
                      int input = s.nextInt();
                      answer.add(input);
                  }
      
                  // Kaboom
                  System.out.println(answer);
                  System.out.println(answer.indexOf(1));
                  System.out.println(answer.indexOf(2));
                  System.out.println(answer.indexOf(3));
                  System.out.println(answer.indexOf(4));
                  System.out.println(answer.indexOf(5));
                  System.out.println(answer.indexOf(6));
              }
      
          }
      

      【讨论】:

        猜你喜欢
        • 2015-07-18
        • 1970-01-01
        • 2015-05-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多