java 将 txt 文件中的数据放入二维数组中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16379730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 22:48:53  来源:igfitidea点击:

take data from a txt file into a 2D array

javaarraysjava.util.scanner

提问by Joseph-Student in SWE 1st Year

Thank you all very much for your help both answers work Perfectly. I have a Lab assigment for Java that I need some help with please.
The assignemnt is to Read a txt that has 28 ints and then place them into a 2D Array. Here is what I have:

非常感谢大家的帮助,两个答案都完美地工作。我有一个 Java 实验室作业,我需要一些帮助。
任务是读取一个有 28 个整数的 txt,然后将它们放入一个 2D 数组中。这是我所拥有的:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class TextFileExample {
    public static void main(String[] args) {

        String fileName = "TemperatureData.txt";
        Scanner inputStream = null;
        System.out.println("The file " + fileName + "\ncontains the following lines:\n");
        try
        {
            inputStream = new Scanner(new File("C:\Users\username\Documents\TemperatureData.txt"));//The txt file is being read correctly.
            String line = inputStream.nextLine();
            String[] numbers = line.split("");
            int[][] temperatures = new int[4][7];
            for (int row = 0; row < 4; row++) {
                for (int column = 0; column < 7; column++) {
                    temperatures[row][column] = temperatures.parseInt(temperatures[row][column]);//Is this correct?
                }
            }
        }
        catch(FileNotFoundException e)
        {
            System.out.println("Error opening the file " + fileName);
            System.exit(0);
        }

        while (inputStream.hasNextLine())
        {
            String line = inputStream.nextLine();
            System.out.println(line);
        }
        inputStream.close();

    }
}

I need some help as to Getting the data from the txt file into the array temperatures[4][7].

关于从 txt 文件中获取数据到数组中,我需要一些帮助temperatures[4][7]

Then printout the temperatures array showing the data. The output should be as follow:

然后打印出显示数据的温度数组。输出应如下所示:

Temperature Data

Week 1: 73 71 68 69 75 77 78

Week 2: 76 73 72 72 75 79 76

Week 3: 79 82 84 84 81 78 78

Week 4: 75 72 68 69 65 63 65

He does not required the txt file to be an output.

他不需要 txt 文件作为输出。

The txt file is this:

txt文件是这样的:

73
71
68
69
75
77
78
76
73
72
72
75
79
76
79
82
84
84
81
78
78
75
72
68
69
65
63
65

回答by durron597

temperatures[row][column] = temperatures.parseInt(temperatures[row][column]);

Should be

应该

temperatures[row][column] = Integer.parseInt(line);

Also, delete String[] numbers = line.split("");as it is not used.

此外,删除,String[] numbers = line.split("");因为它不使用。



You need to reorder a few more things for this to work. The logic is:

您需要重新排序一些东西才能使其正常工作。逻辑是:

Open file
if filenotfound, quit
loop through your arrays
    Parse the string in the file, put it in your array

Now you can do something with the array. Note this does not elegantly handle the case where the file isn't long enough.

现在你可以对数组做一些事情。请注意,这并不能优雅地处理文件不够长的情况。

String fileName = "TemperatureData.txt";
Scanner inputStream = null;
System.out.println("The file " + fileName + "\ncontains the following lines:\n");
try
{
  inputStream = new Scanner(new File("C:\Users\username\Documents\TemperatureData.txt"));//The txt file is being read correctly.
}
catch(FileNotFoundException e)
{
  System.out.println("Error opening the file " + fileName);
  System.exit(0);
}

for (int row = 0; row < 4; row++) {
  for (int column = 0; column < 7; column++) {
    String line = inputStream.nextLine();
    int[][] temperatures = new int[4][7];
    temperatures[row][column] = Integer.parseInt(line);
  }
}
inputStream.close();

回答by CronbachAlpha

Here is a solution:

这是一个解决方案:

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.InputMismatchException;
import java.util.Scanner;
import java.util.StringTokenizer;

public class TxtTo2DArray {
public static void main(String[] args) throws FileNotFoundException, IOException {

    String filename = ""; //Here you must write the path to the file f.exp "//folder//file.txt"

    try{
    FileReader readConnectionToFile = new FileReader(filename);
    BufferedReader reads = new BufferedReader(readConnectionToFile);
    Scanner scan = new Scanner(reads);

    int[][] temperatures = new int[4][7];
    int counter = 0;
    try{
        while(scan.hasNext() && counter < 5){
            for(int i = 0; i < 4; i++){
                counter = counter + 1;
                for(int m = 0; m < 7; m++){
                    temperatures[i][m] = scan.nextInt();
                }
            }
    }

            for(int i = 0; i < 4; i++){
            System.out.println("Temperature at week:" + (i + 1) + " is: " + temperatures[i][0] + ", " + temperatures[i][1] + ", " + temperatures[i][2] + ", " + temperatures[i][3] + ", " + temperatures[i][4] + ", " + temperatures[i][5] + ", " + temperatures[i][6]);

            }

    } catch(InputMismatchException e){
        System.out.println("Error converting number");
    }
    scan.close();
    reads.close();
    } catch (FileNotFoundException e){
        System.out.println("File not found" + filename);
    } catch (IOException e){
        System.out.println("IO-Error open/close of file" + filename);
    }

}     
}

With your file gave this results:

您的文件给出了以下结果:

Temperature at week:1 is: 73, 71, 68, 69, 75, 77, 78
Temperature at week:2 is: 76, 73, 72, 72, 75, 79, 76
Temperature at week:3 is: 79, 82, 84, 84, 81, 78, 78
Temperature at week:4 is: 75, 72, 68, 69, 65, 63, 65