Java - putting comma separated integers from a text file into an array

徘徊边缘 提交于 2019-12-25 02:54:00

问题


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

回答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.

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:

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("[,]+");

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.

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


来源:https://stackoverflow.com/questions/30064763/java-putting-comma-separated-integers-from-a-text-file-into-an-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!