Java 如何创建多维数组以从.txt文件获取输入并将字符串和数字分开存储 公共类数组目录{ 公共静态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 如何创建多维数组以从.txt文件获取输入并将字符串和数字分开存储 公共类数组目录{ 公共静态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,file,text,multidimensional-array,input,Java,File,Text,Multidimensional Array,Input,如何生成以下代码以将字符串拆分为多个片段并将其存储在多维数组中?例如: 正文: 123 abc 456 789 def 101 112 排列 [123][abc][456] [789][def][101][112] 将文本中的数字转换为数字后再存储到数组中。我相信我必须使用整数parsed.Int()。由于不确定如何实现它以及以下更正,您最终将得到正确分割字符串的entries数组 public class ArrayDirectory { public static void main(Str

如何生成以下代码以将字符串拆分为多个片段并将其存储在多维数组中?例如:

正文:

123 abc 456

789 def 101 112

排列

[123][abc][456]

[789][def][101][112]


将文本中的数字转换为数字后再存储到数组中。我相信我必须使用整数parsed.Int()。由于不确定如何实现它

以及以下更正,您最终将得到正确分割字符串的entries数组

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"
 }
}

因此,您现在可以编写自己的循环,并根据需要进行处理。

谢谢。我得到一个错误:线程“main”java.lang.ArrayIndexOutOfBoundsException中的异常:3在mainmethod.main(mainmethod.java:55)java结果:1同样的度量[1]必须有4个元素,这是因为您的第二个输入有4个条目。看看这个:789 def 101 112。您的第一个条目只有3个条目:123 abc 456。因此,要么必须更正,要么使用条目[100][4]。您使用的是数组。数组中元素数量的大小必须相同。这个大小应该等于输入中一行的最大大小。如果特定行有5个元素,则条目应为条目[100][5]。我希望你能理解这一点。我的想法是,当分解并存储在数组中时,一些输入行将有3个元素,一些输入行将有4个元素。当我运行条目[100][4]时,它显示了相同的错误:线程“main”java.lang.ArrayIndexOutOfBoundsException中的异常:4在mainmethod.mainmethod.main(mainmethod.java:31)java结果:1生成成功(总时间:0秒)
public static void main(String args[]) throws FileNotFoundException 
{

String file = ("C:\\array.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())
{
    String [] splittedEntries = new String[3];
    splittedEntries = scan.nextLine().split(" ");

    for( int inx = 0; inx < splittedEntries.length; ++inx )
    {
        entries[i][inx] = splittedEntries[inx];
    }
    i++;

}
}
entries[0] = { 123, abc, 456 };
entries[1] = { 789, def, 101 };