Java - putting comma separated integers from a tex

2019-09-04 07:45发布

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条回答
Bombasti
2楼-- · 2019-09-04 08:19

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;
}
查看更多
The star\"
3楼-- · 2019-09-04 08:23

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();
  }    
查看更多
The star\"
4楼-- · 2019-09-04 08:23

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;
}
查看更多
我欲成王,谁敢阻挡
5楼-- · 2019-09-04 08:31

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]);
}    
查看更多
登录 后发表回答