I am trying to write a program to perform matrix operations. I want input the two matrices from a file. The file has the format:

1 2 3 4
5 6 7 8
9 0 1 2
3 4 5 6
*
0 9 8 7
6 5 4 3
2 1 4 3
7 6 4 0

The star is there to show where one matrix ends and the other begins. After a lot of playing around I finally got the first matrix to be put into a 2 dimensional array. I can't figure out how to get the second to be put into another 2D array.

boolean star = false;
            
            Scanner f = new Scanner(new File("C:/Users/Joe/"
                 + "Documents/NetBeansProjects/ClassWork/src/matrixInput.txt"));
            int A[][] = new int[4][4];
            int B[][] = new int[4][4];
                
                while(f.hasNextLine())
                {         
                    for(int row=0; row<=3; row++)
                    {
                        for(int col=0; col<=3; col++)
                        {
                                A[row][col] = f.nextInt();
                                System.out.print(A[row][col] + " ");                        
                        }
                        System.out.println("");                      
                    }
                    
                    if (f.nextLine().equals("*"))
                    {
                        star = true;
                    }
                    
                    for(int row=0; row<=3; row++)
                    {
                        for(int col=0; col<=3; col++)
                        {
                                B[row][col] = f.nextInt();
                                System.out.print(B[row][col] + " ");                        
                        }
                        System.out.println("");                      
                    }
                    
                }
                f.close();
        }catch(Exception e){
            System.err.println("Error: " + e.getMessage());
        }

I get the first array printed and then a Error: null message.

Thanks for anyone that can help.

Add e.printStackTrace(); in your catch block, so you can see more details of the error.

Add e.printStackTrace(); in your catch block, so you can see more details of the error.

output after adding e.printStackTrace()

1 2 3 4
5 6 7 8
9 0 1 2
3 4 5 6
java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:909)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextInt(Scanner.java:2160)
at java.util.Scanner.nextInt(Scanner.java:2119)
at matrix.main(matrix.java:47)
Error: null
BUILD SUCCESSFUL (total time: 1 second)

Basically what is happening here is that your second forloop is reading in the next character as an Int, as specified by the nextInt() method, but of course, the next character is '*', and so is a "mismatch".

Removing the star and just using an empty line would fix this, or maybe skipping the line, or reading it as something else, so that the next loop does not think it is next to be read.

I also have a couple of suggestions:

Firstly, as all you (should be) reading are Ints, using the f.hasNextInt() method for your while loop condition would probably be more of a benefit and secondly, instead of repeating the forloop, you could just create a new function which does the job for you.

However, this is up to you, but here is what i would suggest you could do:

Scanner f = new Scanner(new File("./matrices.txt"));
			    int A[][] = new int[4][4];
			    int B[][] = new int[4][4];
		 	    
			    	
				while(f.hasNextInt())
				{         
				    for(int row=0; row<=3; row++)
				    {
				        for(int col=0; col<=3; col++)
				        {
				                A[row][col] = f.nextInt();
				                System.out.print(A[row][col] + " ");                        
				        }
				        System.out.println("");                      
				    }
		 		
				    System.out.println("");
				    for(int row=0; row<=3; row++)
				    {
				        for(int col=0; col<=3; col++)
				        {
				                B[row][col] = f.nextInt();
				                System.out.print(B[row][col] + " ");                        
				        }
				        System.out.println("");                      
				    }
		 
				}
				f.close();

Oh and you'd need to simply replace the * line in the file with a blank line.
If you are adamant on keeping the break character, I would structure it a bit differently, but as you hard coded the forloops and arrays i assume you already know the size of each input matrix, so you don't need the star there.

I do want to have the program take in any size matrix. I just was trying to get it to work first with the 4x4.

I would also test for the "*" on the next line and break out of the loop if found.

Okay, fair enough.
In that case, how do you propose to get the values for your for loops when taking in matrices of any size?
Surely you need to read in the file first, to find out how big the matrix will be beforehand? otherwise your solution will not work.

I'm not sure but if i were doing this i'd probably read in all the lines of the file into a string arraylist, and then parse them with a foreach loop. This way you keep the scanner consistent and don't have any errors with File I/O, and it's easy to check if a line is "*" or not.

If you have a better idea of course, then ignore me, i haven't read around the problem enough to find a perfect solution.

Yes, for an unknown size, you would read to the end of the file counting the width (by commas) and the length to determine the size of the 2d array.
You would then "new" your array to the given size and load it.

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.