Java 如何创建多维数组以从.txt文件获取输入

Java 如何创建多维数组以从.txt文件获取输入,java,input,multidimensional-array,Java,Input,Multidimensional Array,我试图创建一个大小为[3]和[100]的2d数组,其想法是它将存储三个字符串,这些字符串将存储在一个文本文件中,由tab分隔。我在网络上搜寻过这方面的帮助,但没有找到任何好的答案。它需要是通过Java存储的数组 应该是这样的: 将字符串文件与filename.txt关联 创建大小为3列和100行的二维阵列。在这里,它应该存储从左到右的数据,每行上有3个字符串,然后分别放在新行上 然后我需要一种方法将文本文件中的字符串添加到数组中。最后它应该打印数组的内容 导入java.io.Buffered

我试图创建一个大小为[3]和[100]的2d数组,其想法是它将存储三个字符串,这些字符串将存储在一个文本文件中,由tab分隔。我在网络上搜寻过这方面的帮助,但没有找到任何好的答案。它需要是通过Java存储的数组

应该是这样的:

  • 将字符串文件与filename.txt关联
  • 创建大小为3列和100行的二维阵列。在这里,它应该存储从左到右的数据,每行上有3个字符串,然后分别放在新行上
  • 然后我需要一种方法将文本文件中的字符串添加到数组中。最后它应该打印数组的内容
导入java.io.BufferedReader;
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.FileNotFoundException;
导入java.io.FileReader;
导入java.io.InputStream;
导入java.io.InputStreamReader;
导入java.util.*;
公共类数组目录{
公共静态void main(字符串参数[]){
字符串文件=(“directory.txt”);
//初始化链接到字符串文件的文件
字符串[][]项=新字符串[3][50];
//创建包含3列50行的二维阵列。
openTextFile(文件);
//应该从文件中读取
String line=readFromFile.readLine();
整数计数=0;
while(行!=null){
输入[count]=String.split(“”);
Entry=readFromFile.readline();
}
对于(int h=0;h
导入java.io.BufferedReader;
导入java.io.FileReader;
导入java.io.IOException;
公共类数组目录{
公共静态void main(字符串args[])引发IOException{
BufferedReader readFromFile=新的BufferedReader(新文件读取器(“test.txt”);
整数计数=0;
字符串[][]项=新字符串[100][3];
String line=readFromFile.readLine();
while(行!=null){
//我不知道你的文件的布局,但我猜是:“XX YY ZZ”,中间有空格。
String[]temp=line.split(“”;//此处的温度为:[XX][YY][ZZ]
对于(int i=0;i
阅读有关字符串拆分的信息:

此外,变量应始终以小写字母开头,只有类的第一个字母应该是大的。

有几点:

  • 你没有办法读这个文件
  • 当您的注释声明相反时,您的数组有3行50列
  • 您试图在一个空格上拆分,而不是在选项卡上拆分
请参见代码中的注释,解释其功能:

public class ArrayDirectory {
public static void main(String args[]) throws FileNotFoundException {
    String file = ("lab4b2.txt");
    Scanner scan = new Scanner(new FileReader(file));
    // initialises the scanner to read the file file

    String[][] entries = new String[100][3];
    // creates a 2d array with 100 rows and 3 columns.

    int i = 0;
    while(scan.hasNextLine()){
        entries[i] = scan.nextLine().split("\t");
        i++;
    }
    //loops through the file and splits on a tab

    for (int row = 0; row < entries.length; row++) {
        for (int col = 0; col < entries[0].length; col++) {
            if(entries[row][col] != null){
                System.out.print(entries[row][col] + " " );
            }
        }
        if(entries[row][0] != null){
            System.out.print("\n");
        }
    }
    //prints the contents of the array that are not "null"
}
}
公共类数组目录{
公共静态void main(字符串args[])引发FileNotFoundException{
字符串文件=(“lab4b2.txt”);
扫描仪扫描=新扫描仪(新文件读取器(文件));
//初始化扫描仪以读取文件
字符串[][]项=新字符串[100][3];
//创建一个包含100行和3列的二维阵列。
int i=0;
while(scan.hasNextLine()){
条目[i]=scan.nextLine().split(“\t”);
i++;
}
//在文件中循环并在选项卡上拆分
for(int row=0;row
无论如何显示你的代码,我们不会模仿:)无论如何发布你的代码。人们不会因此嘲笑你,他们只会帮助你指出你可能错在哪里。如果看不到代码,也很难提供帮助。我已经发布了我现在得到的。我对Java很糟糕,只不过我真的用过JS和CSS等。所以它在很多地方可能是错误的,
readFromFile
input
初始化了吗?我还没有这样做。这是从一个不同的例子,我试图把它放在一起真的很快,我已经通过了这么多不同的代码段,这是目前我已经。。。我很抱歉。谢谢你。我想这对我的工作会很好。但是我在ArrayDirectory.main(ArrayDirectory.java:18)的线程“main”java.lang.NullPointerException中收到以下错误异常。请重试,我的代码中没有收到任何异常。如果文件不在您的工作区文件夹中,请确保它是文件的绝对路径。它现在只读取文件中的前3个字符串。@Sionnach733似乎已经开始工作了,但我想我更了解2d阵列的工作原理,需要它来存储打印出来,比如位置00,01,02,然后是新行,然后是10,11,12,等等,直到文件工作结束,这只是我的一个输入错误。太完美了。谢谢顺便说一句,你知道为什么我的eclipse在文本文件的每个字母之间都放了一个框吗?文件总是有100行吗?编辑:这只打印位置0,1,0,2和0,3。知道为什么吗?文件中的行数目前只有4行,但是如果添加了另一条记录,那么数组的大小将不得不增加。所以我把它设为100,因为我认为这就足够了。它只打印文本文件中的第一条记录:(它为我打印全部,在你的问题中显示一个文件内容的示例。)
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;


public class ArrayDirectory{

        public static void main(String args[]) throws IOException {
            BufferedReader readFromFile = new BufferedReader(new FileReader("test.txt"));


            int count = 0;
            String[][] entry = new String[100][3];

            String line = readFromFile.readLine();



            while (line != null) {
                //I don't know the layout of your file, but i guess it is: "XX YY ZZ", with spaces inbetween.
                String[] temp = line.split(" "); //temp here will be: [XX][YY][ZZ]
                for (int i = 0; i < temp.length; i++) {
                    entry[count][i] = temp[i];
                }
                line = readFromFile.readLine();
                count++;


            }
            readFromFile.close();
            for (int j = 0; j < entry.length; j++) {
                for (int k = 0; k < entry[0].length; k++) {

                    System.out.print(entry[j][k] + " ");
                }
                System.out.println();
            }

        }
}
public class ArrayDirectory {
public static void main(String args[]) throws FileNotFoundException {
    String file = ("lab4b2.txt");
    Scanner scan = new Scanner(new FileReader(file));
    // initialises the scanner to read the file file

    String[][] entries = new String[100][3];
    // creates a 2d array with 100 rows and 3 columns.

    int i = 0;
    while(scan.hasNextLine()){
        entries[i] = scan.nextLine().split("\t");
        i++;
    }
    //loops through the file and splits on a tab

    for (int row = 0; row < entries.length; row++) {
        for (int col = 0; col < entries[0].length; col++) {
            if(entries[row][col] != null){
                System.out.print(entries[row][col] + " " );
            }
        }
        if(entries[row][0] != null){
            System.out.print("\n");
        }
    }
    //prints the contents of the array that are not "null"
}
}