i have a program that requires that I load info from a text file and in the end, make an arena out of it. The text file is in the following format:

<number of rows in body>
<number of cols in body>
<body that is rows*cols big>

ex:
15
15
0,0,0,0,0,0,0,1,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0,0,2,0,0,0
0,0,0,0,0,0,0,0,0,0,0,2,0,0,0
0,2,0,0,0,2,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0,0,2,0,0,0
0,0,0,0,2,0,0,0,0,0,0,2,0,0,0
0,2,0,0,0,0,0,0,0,0,0,2,0,0,0
0,2,0,0,0,2,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0,0,2,0,0,0
0,0,0,0,2,0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0,0,2,0,0,0
0,0,0,0,0,0,0,0,0,0,0,2,0,0,0
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
0,2,0,0,0,2,0,0,0,0,0,2,0,0,0
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0

how would i go about putting the body into a 2D array?

Reading from a file:

File file = new File("filename");
        BufferedReader reader = null;
        
        try {
            reader = new BufferedReader(new FileReader(file));
            
            String line = reader.readLine();
            int lineNum = 0;
            while (line!=null) {
                lineNum++;
                // DO THINGS WITH LINE
                System.out.println("Line "+lineNum+"="+line);
                
                
                // ALWAYS READ THE NEXT LINE AS THE LAST COMMAND OF THE LOOP
                line = reader.readLine();
            }
        } catch (IOException e) {
            System.out.println(e.getMessage());
        } finally {
            if (reader!=null) reader.close();
        }

Using the lines of the file

// The first 2 lines will give you the size of the array:
int row = 0;
int col = 0;
int lineNum = 0;
int [][] array = null;

while (...) {
    lineNum++;
   if (lineNum==1) {
     // take the first line and put it into the row:
     row = Integer.parseInt(line);
   } else if (lineNum==2) {
     // take the second line and put it into the col:
      col = Integer.parseInt(line);
      array = new int[row][col];
   } else {
        String [] tokens = line.split(",");
        for-loop the tokens array {
            array[..][..] = tokens[..];
        }
   }

// READ THE NEXT LINE
}

The split method takes the line and returns an array with elements the values of the String separated by the ',':

String s = "1,2,3,4";
String [] tokens = s.split(",");
// tokens now has: {1, 2, 3, 4}

So at the else statement, use a for-loop to take the elements of each row and put them into the 2D array. Each row of the 2D array will have the elements of the tokens array. With each while loop you will increase the index of the row of the 2D array and in the for loop you will increase the col of the 2D array which will be the same as the tokens array

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.