0

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 
4
  • int size = input.scan.nextInt(); will get you the first number in which you do int[][] matrix = new int[size][size]; Then you continue to read until size times and then you go back to the start if there is a nextInt Commented Mar 29, 2016 at 3:03
  • how would you use this to jump from the first matrix to the second. To be more clear, int size = input.scan.nextInt(); will get 2, then the loop will continue and put the 2,3,4,9 into the array. how can i then redo this loop and allocate the next array to size 3? my question is on the reading of the file more than the array setup. Commented Mar 29, 2016 at 3:06
  • so you can do at the end of all that code if(input.hasNextInt()) continue; but it will destroy that array and the variable size so you might want to differiant somehow Commented Mar 29, 2016 at 3:08
  • @3kings i edited my code with what you suggested but output is still incorrect. could you take a look? Commented Mar 29, 2016 at 3:43

1 Answer 1

1

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();
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your help. perfect. As this is for class, could you explain how you did that? @jrhee17
I can explain how my answer differs with your edited code. Getting the size should be inside of the while loop, since the size will change for each array. Try moving your int size = input.nextInt(); inside of your while loop

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.