-1

非常感谢大家的帮助,两个答案都很完美。我有一个 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
4

2 回答 2

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();
于 2013-05-04T23:08:37.963 回答
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
于 2013-05-04T23:37:41.433 回答