I'm getting an error about dereferencing in the following code (I've commented out where it gets caught up)
The main method works fine, but the Mply method is causing me lots of headaches. Hope this is sort of clear.
//multiply matrixes
import java.io.FileInputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.*;
import java.util.StringTokenizer;
public class Assignment1 {
public static void main(String[] args){
int a=5;
int b=2;
int x[][] = new int[a][b];
for(int i=0;i<a;i++)
for(int j=0;j<b;j++)
x[i][j]=0;
System.out.println(x[1][1]);
/*
for(int j= 0; j<
for (int i = 0; i < array.length; i++)
array[i] = s.nextInt();
*/
// read from file
FileInputStream fstream=null;
try{
fstream = new FileInputStream("C:\\javastuff\\principles\\input.txt");
}
catch (Exception e){
System.out.println(e.getMessage());
}
try (BufferedReader br = new BufferedReader(new InputStreamReader(fstream))) {
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
/* StringTokenizer tokenizer = new StringTokenizer(line, " ");
while (tokenizer.hasMoreElements()) {
// parse each integer in file
int i = Integer.parseInt(tokenizer.nextToken());
System.out.println("Number " + i);
}
*/
}
br.close();
}
catch(Exception e){
}
// write to file
BufferedWriter outputWriter = null;
try{
outputWriter = new BufferedWriter(new FileWriter("C:\\javastuff\\principles\\output.txt"));
for (int i = 0; i < a; i++) {
outputWriter.write(Integer.toString(x[i][0]));
outputWriter.newLine();
}
outputWriter.flush();
outputWriter.close();
}
catch (Exception e){
}
}
public static void Mply(int x[][], int a, int b){ //multiplies arraries and checks if they are same values
int aRows = a.length;
int aColumns = a[0].length;//starts dereferecning herer
int bRows = b.length;
int bColumns = b[0].length;
for (int i=0; i<a; ++i)
for (int j=0; j<b; ++j)
for (int k=0; k<a[0][0].length(); ++k)
x[i][k] += a[i][k] * b[k][j];
}
}
based on the method header for Mply
public static void Mply(int x[][], int a, int b)
The type of x
is int[][]
, the type of a
is int
, and the type of b
is int
. Specifically, note that a
and b
are not arrays.
Thus trying to access them as arrays by using a[0]
is illegal.
If you want a
and b
to be 2-dimensional arrays, you have to update your method header to say that.
public static void Mply(int[][] x, int[][] a, int[][] b)