Cfs0004
Cfs0004

Reputation: 85

Java Matrix Array txt file

So I have a text file that has 3 matrices on it as follows:

2
2 3
5 9

3
3 -2 4
-1 5 2
-3 6 4

4
2 4 5 6
0 3 6 9
0 0 9 8
0 0 0 5

I need to read this file and put it into an array. As you can see from the above, the number of n x n is given (2,3,4). I was wondering if there was a way to read this and then allocate an array without reading the below twice? Once I read the txt file i will need to do some computations with the array and also print it out.

public class tester{

  public static void main(String[] args) {
     // TODO Auto-generated method stub         
     try {
         Scanner input = new Scanner(new File(lab2-input.txt"));

         int size = input.nextInt();
         int rowSize = size;
         int columnSize = size;
         int[][] a = new int[size][size];
         System.out.println("Size: " + size);

         while (input.hasNextLine()) {
             for (int i = 0; i < rowSize; i++) {
                 for (int j = 0; j < columnSize; j++) {

                    try{
                     a[i][j] = input.nextInt();

                     }
                    catch (java.util.NoSuchElementException e) {
                        // e.printStackTrace();
                     }
                 }
             }         //print the input matrix
             System.out.println("The input sorted matrix is : ");
             for (int i = 0; i < rowSize; i++) {
                 for (int j = 0; j < columnSize; j++) {

                    System.out.printf("%5d ", a[i][j]);
                 }
                 System.out.println();

             }if(input.hasNextInt()) continue;
         }
     } catch (Exception e) {
         e.printStackTrace();
     }
 }}

the output i am currently getting is:

 Size: 3
 The input sorted matrix is : 
 3    -2     4 
-1     5     2 
-3     6     4 
 The input sorted matrix is : 
 4     2     4 
 5     6     0 
 3     6     9 
 The input sorted matrix is : 
 0     0     9 
 8     0     0 
 0     5     9 

Upvotes: 0

Views: 1205

Answers (1)

jrhee17
jrhee17

Reputation: 1152

Does this do what you want?

public static void main(String[] args) {
    // TODO Auto-generated method stub
    try {


        // Read input file
        Scanner input = new Scanner(new File("lab2-input.txt"));

        while (input.hasNextInt()) {

            // This should be here to get size of array before getting each array
            int size = input.nextInt();
            int[][] a = new int[size][size];

            for (int i = 0; i < size; i++) {
                for (int j = 0; j < size; j++) {

                    try{
                        a[i][j] = input.nextInt();

                    }
                    catch (java.util.NoSuchElementException e) {
                        // e.printStackTrace();
                    }
                }
            }

            //print the input matrix
            System.out.println("The input sorted matrix is : ");
            for (int i = 0; i < size; i++) {
                for (int j = 0; j < size; j++) {
                    System.out.printf("%5d ", a[i][j]);
                }
                System.out.println();

            }

        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Upvotes: 1

Related Questions