从文件读取到2D数组 [英] reading from file into 2D array

查看:74
本文介绍了从文件读取到2D数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须从这样的文件中读取数据:

I have to read data from a file like this:

4
192 48 206 37 56
123 35 321 21 41
251 42 442 32 33

第一个数字是候选人(列)的总数,我需要存储该值以用于其他用途。然后,我需要将其余数据读取到2D数组中。我用现在的代码更新了代码,但仍然无法正常工作。我不断收到错误
java.util.NoSuchElementException:找不到行

the first number is the total number of candidates(columns) and i need to store that value for other use. Then i need to read the rest of the data into a 2D array. I updated my code with what i have now but it still not working. I keep getting the error java.util.NoSuchElementException: No line found

 public static int readData(int[][] table, Scanner in)throws IOException
{
System.out.println("Please enter the file name: ");
 String location = in.next();
 Scanner fin = new Scanner(new FileReader(location));
 int candidates = fin.nextInt();
 fin.nextLine();
for (int row = 0; row < 5; row++) {
  for (int column = 0; column < candidates; column++) {
    String line = fin.nextLine();
    fin.nextLine();
    String[] tokens = line.split(" ");
    String token = tokens[column];
    table[row][column] = Integer.parseInt(token);
  }
}
fin.close();
return candidates;
}

}


推荐答案

据我了解,您的主要任务是从文件中提取整数值并将其放入2D数组中。

As far as I understand, your main task is to extract integer values from file and put it into the 2D array.

我建议您参考Oracle网站上的Scanner API参考:
https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html

I would recommend you refering to the Scanner API Reference on Oracle website: https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html

您可以在此处找到一些更适合您任务的方法:

You can find there, that there are some more suitable methods for your task:


  1. nextInt() -用于直接从文件中获取整数值

  2. hasNextLine()-用于确定是否存在下一行输入

  1. nextInt() - used to get integer value directly from file
  2. hasNextLine() - used to determine if there is next line of input

假设候选人列数,则应将其视为整数,而不是字符串:

Assuming, that candidates is the number of columns, it should be treated as integer, not string:

int candidates = fin.nextInt();

使用提到的Scanner方法,获得 String 不再需要文件中的值,因此 line numbers 变量可能会从源代码中完全删除。

With use of mentioned Scanner methods, getting String values from file won't be needed anymore, so line and numbers variables may be completely removed from source code.

使用 hasNextLine()方法,可以肯定的是,该文件将被读取到结尾:

With use of hasNextLine() method, you can be sure, that file will be read till its end:

int row = 0;
while(fin.hasNextLine()) {                         //while file has more lines
    for(int col = 0; col < candidates; j++) {      //for 'candidates' number of columns
        table[row][col] = fin.nextInt();           //read next integer value and put into table
    }
    row++;                                         //increment row number
}

请记住, Java数组不是动态可扩展

您的2D数组-应该初始化为要放入其中的数据的确切大小。

Your 2D array - table should be initialized with exact size of data to be put inside.

在您当前的示例中,您不知道直到文件末尾的输入行数,因此正确初始化数组可能需要其他操作。

In your current example you don't know the number of input rows until the end of file, so proper initialization of array may require additional operations.

这篇关于从文件读取到2D数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆