【问题标题】:Outcome of array is null?数组的结果为空?
【发布时间】:2012-10-04 11:27:14
【问题描述】:

我不久前发布了一个问题,该问题得到了回答并帮助了我很多。抱歉这么快又发帖了。 =/

我已经检查过了,不确定我做错了什么,打印的结果是几个“空”字。我正在尝试使用一种方法用文本文件中的数据填充二维数组。然后在主类中运行该方法以打印出填充的数组。

任何帮助将不胜感激,在此先感谢您。 =)

主类:

public static void main(String[] args) throws IOException {

    ScoreProcessor scores = new ScoreProcessor();

    String[][] table = scores.readFile();


    for (int row = 0; row < table.length; row++) {
        for (int col = 0; col < table[row].length; col++) {
            System.out.print(table[row][col] + "\t");
        }
    }
}

另一个类,包含方法:

    public class ScoreProcessor {

    static public String[][] readFile() throws IOException {

        File filedata = new File("src/JavaApp2/Data.txt");
        Scanner file = new Scanner(filedata);

        int row = 0, col = 0;

        String[][] scores = new String[8][5];


        while (file.hasNext()) {

            Scanner readfile = new Scanner(filedata);
            readfile.nextLine();
            readfile.useDelimiter(",");

            while (readfile.hasNext(",")) {
              String line =  readfile.next();


             line = scores[row][col] ;
                col++;
            } // end innerloop
            row++;
            col = 0;


        } // end outerloop

        return scores;
    } // end method
} // end class

数据文件

Student,Q1,Q2,Q3,Q4
abcd0001,50,55,59,72
efgh0002,82,78,84,86
ijkl0003,42,45,46,52
mnop0004,66,74,72,78
qrst0005,82,86,89,88
uvwx0006,77,83,87,82
yzab0007,62,64,70,68

【问题讨论】:

  • 你的 Data.txt 文件是什么样子的?
  • 我认为,您的内部循环不会读取 after 最后一个逗号的条目。会不会是个问题?
  • 请也向我们展示输出。是每个元素 null 还是只有一个字段?
  • 为什么要开2个扫描仪???

标签: java arrays methods


【解决方案1】:

正如其他人指出的那样,file.hasNextInt() 返回 false,因为您正在阅读的行不是 int。由于它为空,因此您的代码永远不会进入循环并且不会提供数据。

   Scanner readfile = new Scanner(filedata);
   String line1 = readfile.nextLine();
   readfile = readfile.useDelimiter(",");

不要再次打开文件,只需读取每一行并用','分割它

String line = file.nextLine();

for (String each: line.split(",")) {
   scores[row][col] = each;
}

【讨论】:

  • 非常感谢大家。它现在似乎按预期工作。 :)
【解决方案2】:

线

while (file.hasNextInt())

将返回false。因此,您的数组将不会使用数据初始化并返回 null 打印。

我建议您检查readfile.nextLine() 是否返回null

【讨论】:

    【解决方案3】:

    试试这个:

     public class ScoreProcessor {
    
            public static String[][] readFile() throws IOException {
    
                File filedata = new File("src/JavaApp2/Data.txt");
                Scanner file = new Scanner(filedata);
    
                int row = 0;
    
                String[][] scores = new String[8][5];
    
                while(file.hasNextLine()){
                    String line=file.nextLine();
                    scores[row]=line.split(",");
                    row++;
                }
    
                return scores;
            } 
    
            public static void main(String[] args) throws IOException {
    
                String[][] table = readFile();
    
                for (int row = 0; row < table.length; row++) {
                    for (int col = 0; col < table[row].length; col++) {
                        System.out.print(table[row][col] + "\t");
                    }
                    System.out.print("\n");
                }
            }
    
        }
    

    【讨论】:

      【解决方案4】:

      查看hasNextInt() 的定义。如果下一个标记不可解释为 int,它将返回 false。

      在我看来,您的文件以“Student”开头,或者如果您跳过标题行,则以“abcd0001”开头,但不是 int。所以hasNextInt() 将立即返回 false,并且您的内部循环永远不会被执行。

      除此之外,我不清楚你为什么要实例化第二个Scanner

      这样的问题可以很容易地用调试器跟踪,我建议你熟悉这种方法:)

      【讨论】:

        【解决方案5】:

        使用 file.hasNext() insted of file.hasNextInt() 应该可以解决问题

        【讨论】:

          【解决方案6】:

          这不也是(除了HasNextInt())问题的核心吗:

          line = scores[row][col] ;
          

          应该是其他方式。这样你的数组将永远不会被填充,当然总是只包含空值。

          scores[row][col] = line;
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2015-08-26
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-08-25
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多