Java - 将逗号分隔的整数从文本文件放入数组中

时间:2022-09-06 00:16:40

Had a quick question based on an assignment I'm working on.

有一个基于我正在进行的任务的快速问题。

I have a list of numbers like this in a text file:

我在文本文件中有这样的数字列表:

5, 8, 14
7, 4, 2

And I need them inserted into an array as such

我需要将它们插入到数组中

joe[5][8] = 14; 
joe[7][4] = 2; 

Having trouble getting this done, thanks to anyone who can give me a hand.

无法完成任务,感谢任何能够帮助我的人。

-Edit-

This is the code I have now

这是我现在的代码

File f = new File("solution.txt");
Scanner sc = new Scanner(f);      

while(sc.hasNextLine())
{
    if(sc.hasNextInt())
        x = sc.nextInt();

    if(sc.hasNextInt())
        y = sc.nextInt();

    if(sc.hasNextInt())
        joe[x][y] = sc.nextInt();
}    

4 个解决方案

#1


Try instantiating a 2 dimensional array then reading each line, and then each of the three numbers from each line. Use the first 2 numbers as the array indices and the last as the value.

尝试实例化一个二维数组,然后读取每一行,然后从每一行中的三个数字中的每一个。使用前2个数字作为数组索引,使用最后一个数字作为值。

Some example code

一些示例代码

int[][] numbers = new int[1000][1000];
File fin = new File("filename");
BufferedReader br = new BufferedReader(new FileReader(fin));

String line = null;
while ((line = br.readLine()) != null) {
    String tokens[] = line.split(", ");
    int numberOne = Integer.parseInt(tokens[0]);
    int numberTwo = Integer.parseInt(tokens[1]);
    int numberThree = Integer.parseInt(tokens[2]);
    numbers[numberOne][numberTwo] = numberThree;
}

#2


Take an entire line an split the line into a String array using the method split as exemplified below. Then take each element of the String array and convert it to an int using the method Integer.parseInt( ). Your code should look something like:

使用如下所示的方法拆分整行,将该行拆分为String数组。然后获取String数组的每个元素,并使用Integer.parseInt()方法将其转换为int。您的代码应该类似于:

File f = new File("solution.txt");
Scanner sc = new Scanner(f);      

while(sc.hasNextLine())
{
    String line = sc.nextLine();
    String[] number = line.split(", ");
    int x = Integer.parseInt(number[0]);
    int y = Integer.parseInt(number[1]);
    joe[x][y] = Integer.parseInt(number[2]);
}    

#3


You have to specify the , as a separator.

您必须指定,作为分隔符。

Try this: sc.useDelimiter("[,]+");

试试这个:sc.useDelimiter(“[,] +”);

Then your code is:

然后你的代码是:

File f = new File("solution.txt");
    Scanner sc = new Scanner(f);  
    sc.useDelimiter("[,]+");    


  while(sc.hasNextLine())
  {
    if(sc.hasNextInt())
    x = sc.nextInt();
    else {}
    if(sc.hasNextInt())
    y = sc.nextInt();
    else{}
    if(sc.hasNextInt())
    joe[x][y] = sc.nextInt();
  }    

#4


This may not be the most efficient way but it allows for unknown array distentions.

这可能不是最有效的方法,但它允许未知的阵列distentions。

public int[][] loadArray(String file) {
    File fin = new File(file);
    BufferedReader br = null;
    try {
        br = new BufferedReader(new FileReader(fin));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    int width = 1;
    int[][] numbers = new int[1][1];
    String text = null;
    try {
        while ((text = br.readLine()) != null) {
            String i[] = text.split(", ");
            int a = Integer.parseInt(i[0]);
            int b = Integer.parseInt(i[1]);
            if (a > numbers.length) numbers = new int[a][width];
            if (b > width) {
                numbers = new int[numbers.length][b];
                width = b;
            }
            numbers[a][b] = Integer.parseInt(i[2]);
        }
    } catch (NumberFormatException | IOException e) {
        e.printStackTrace();
    }
    return numbers;
}

#1


Try instantiating a 2 dimensional array then reading each line, and then each of the three numbers from each line. Use the first 2 numbers as the array indices and the last as the value.

尝试实例化一个二维数组,然后读取每一行,然后从每一行中的三个数字中的每一个。使用前2个数字作为数组索引,使用最后一个数字作为值。

Some example code

一些示例代码

int[][] numbers = new int[1000][1000];
File fin = new File("filename");
BufferedReader br = new BufferedReader(new FileReader(fin));

String line = null;
while ((line = br.readLine()) != null) {
    String tokens[] = line.split(", ");
    int numberOne = Integer.parseInt(tokens[0]);
    int numberTwo = Integer.parseInt(tokens[1]);
    int numberThree = Integer.parseInt(tokens[2]);
    numbers[numberOne][numberTwo] = numberThree;
}

#2


Take an entire line an split the line into a String array using the method split as exemplified below. Then take each element of the String array and convert it to an int using the method Integer.parseInt( ). Your code should look something like:

使用如下所示的方法拆分整行,将该行拆分为String数组。然后获取String数组的每个元素,并使用Integer.parseInt()方法将其转换为int。您的代码应该类似于:

File f = new File("solution.txt");
Scanner sc = new Scanner(f);      

while(sc.hasNextLine())
{
    String line = sc.nextLine();
    String[] number = line.split(", ");
    int x = Integer.parseInt(number[0]);
    int y = Integer.parseInt(number[1]);
    joe[x][y] = Integer.parseInt(number[2]);
}    

#3


You have to specify the , as a separator.

您必须指定,作为分隔符。

Try this: sc.useDelimiter("[,]+");

试试这个:sc.useDelimiter(“[,] +”);

Then your code is:

然后你的代码是:

File f = new File("solution.txt");
    Scanner sc = new Scanner(f);  
    sc.useDelimiter("[,]+");    


  while(sc.hasNextLine())
  {
    if(sc.hasNextInt())
    x = sc.nextInt();
    else {}
    if(sc.hasNextInt())
    y = sc.nextInt();
    else{}
    if(sc.hasNextInt())
    joe[x][y] = sc.nextInt();
  }    

#4


This may not be the most efficient way but it allows for unknown array distentions.

这可能不是最有效的方法,但它允许未知的阵列distentions。

public int[][] loadArray(String file) {
    File fin = new File(file);
    BufferedReader br = null;
    try {
        br = new BufferedReader(new FileReader(fin));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    int width = 1;
    int[][] numbers = new int[1][1];
    String text = null;
    try {
        while ((text = br.readLine()) != null) {
            String i[] = text.split(", ");
            int a = Integer.parseInt(i[0]);
            int b = Integer.parseInt(i[1]);
            if (a > numbers.length) numbers = new int[a][width];
            if (b > width) {
                numbers = new int[numbers.length][b];
                width = b;
            }
            numbers[a][b] = Integer.parseInt(i[2]);
        }
    } catch (NumberFormatException | IOException e) {
        e.printStackTrace();
    }
    return numbers;
}