Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将txt文件中的数据放入2D数组_Java_Arrays_Java.util.scanner - Fatal编程技术网

Java 将txt文件中的数据放入2D数组

Java 将txt文件中的数据放入2D数组,java,arrays,java.util.scanner,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) {

非常感谢大家的帮助,两个答案都非常有效。 我有一个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文件作为输出

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
应该是

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();
String fileName=“TemperatureData.txt”;
扫描仪输入流=空;
System.out.println(“文件“+fileName+”\n包含以下行:\n”);
尝试
{
inputStream=new Scanner(新文件(“C:\\Users\\username\\Documents\\TemperatureData.txt”);//txt文件正在正确读取。
}
catch(filenotfounde异常)
{
System.out.println(“打开文件时出错”+文件名);
系统出口(0);
}
用于(int行=0;行<4;行++){
for(int列=0;列<7;列++){
String line=inputStream.nextLine();
int[][]温度=新int[4][7];
温度[行][列]=整数.parseInt(行);
}
}
inputStream.close();
这里有一个解决方案:

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

}     
}

将28个整数转换成2D数组的逻辑是什么。
数据文件是什么样子的?整数是如何分开的?我们假设使用一个包含大量整数的txt文件,每行一个整数。然后将该txt文件写入代码,将num重新输入到4x7数组中,然后打印出数组。请将文本文件编辑到您的帖子中。txt文件错误,每行一个num,总共28行。我刚刚更改了代码。我的问题是,这做了什么,我完成的是温度数组中的数据还是存在更多?我明白了,所以现在我只需要把我的数组做成一个for循环,这样它就可以按需要打印出数组,按行显示所有的数字,对吗?@user2329614是的,但你应该自己做这部分是的,我仍然被卡住了,因为我不确定我在做什么。对Java来说还是新手。我需要使用表格编码还是使用system.out.println()得到的cna谢谢你的帮助它工作得很好,只是不知道如何说这个问题已经得到了回答。你的意思是什么?:P只是更改循环的问题非常感谢你的帮助。有什么不清楚的吗?如果是,就问吧!如果这回答了您的问题,请接受!:)是的,你回答了我的问题,但是因为我是Java新手,所以我使用了top编码,因为我更了解它。如果你说你更了解durron597发布的代码,那么很难看出你两个都不懂。它们的构造是相同的,只是我的代码有一些额外的异常处理程序。正如我所说的,如果有什么不清楚或你不明白,你可以问!
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();
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