0

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();
}    
Jyr
  • 711
  • 5
  • 16
Jane
  • 1
  • 1
  • 2
  • Can you give an example of what the whole arrays should be with this example. – yesennes May 05 '15 at 23:13
  • 4
    "*Having trouble getting this done*" is not precise problem description. What trouble do you have? Can we see your code? BTW to add more info use [edit] option placed below your post. – Pshemo May 05 '15 at 23:14
  • At what point are you getting tied up? Do you have code that reads in the numbers from a file? Are you not sure how to create the array dynamically? – pathfinderelite May 05 '15 at 23:16
  • Read the file line by line and store the 3 numbers in variables `i`, `j` ,`k`, then loop over the array with `joe[i][j] = k`. – user1803551 May 05 '15 at 23:17
  • Have you tried using `Scanner`? Something like `Scanner sc = new Scanner (new FileReader())` ; `sc.useDelimiter("[,]+")` ; `while sc.hasNext()...` Are you looking for a question about this? – Shondeslitch May 05 '15 at 23:19

4 Answers4

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;
}
AlienHoboken
  • 2,750
  • 20
  • 23
1

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]);
}    
Rezwan Azfar Haleem
  • 1,198
  • 13
  • 23
  • 1
    It should be `line.split(", ");` instead of `line.split(",");` – Jyr May 05 '15 at 23:34
  • It does not matter since at the moment Integers are being parsed the white space would be ignored. – Rezwan Azfar Haleem May 05 '15 at 23:36
  • 1
    No, it will throw a `NumberFormatException` since the value represented by the `String` is not an `int` (caused by the whitespace). You should either call `trim()` on the `String`s before parsing or change the regex of the `split()` method (as I suggested earlier). – Jyr May 05 '15 at 23:44
  • You are right. I made a bad mistake of thinking that. I just tried out some code. Thank you for helping me learn this – Rezwan Azfar Haleem May 05 '15 at 23:47
0

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();
  }    
Shondeslitch
  • 1,049
  • 1
  • 13
  • 26
0

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;
}
Caleb Limb
  • 304
  • 1
  • 11