将数据从txt文件转换为2D数组

时间:2013-05-04 23:00:13

标签: java arrays java.util.scanner

非常感谢你们的帮助,这两个答案完美无缺。 我有一个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();

    }
}

我需要一些帮助才能将txt文件中的数据导入数组temperatures[4][7]

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

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

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

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

2 个答案:

答案 0 :(得分:1)

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

应该是

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

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


你需要重新订购一些东西才能使它工作。逻辑是:

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

现在你可以对数组做些什么了。请注意,这并不能很好地处理文件不够长的情况。

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();

答案 1 :(得分:0)

这是一个解决方案:

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);
    }

}     
}

用你的文件给出了这个结果:

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