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.