At first sorry if not everything is understandable (I'm from Germany). And please try to understand that I'm quite new to java and may have done some stupid mistakes. I have two classes. One with the constructor and functions and one with the menu.
Class 1 (constructor and functions):
public class Matrix {
private float[][] matrix;
...
public void createMatrix(int zei, int spa){
...
public void printMatrix(){
int x = 0;
while (x < this.matrix.length){
for (float elem : this.matrix[x]){
System.out.printf("%10d", elem);
}
x++;
System.out.println();
}
System.out.println();
}
Class 2 (Menu):
public class MenuMatrix{
public static void menue(){
...
if (m1 == null){
Matrix m1 = new Matrix();
m1.createMatrix(zei, spa);
}
else if (m2 == null){
Matrix m2 = new Matrix();
m2.createMatrix(zei, spa);
}
else{
System.out.println("Which matrix do you want to replace?");
m1.printMatrix();
m2.printMatrix();
int x = readInteger("Please choose matrix (1) or (2).");
...
I wanted to make sure that only 2 objects of this kind could exist. Therefore I've included the check if the object already exist (m1/m2 == null). There is the first error. In the line with the if-clauses it tells me that "m1/m2 cannot be resolved to a variable". The other error comes in the m1/m2.printMatrix() line. Eclipse says "m1/m2 cannot be resolved". Informations on a few different sites have told me that it should work this way. I hope you can tell me where exactly the problem is.
Thanks in advance.
Looking at your code sample:
if (m1 == null){
Matrix m1 = new Matrix();
m1.createMatrix(zei, spa);
}
else if (m2 == null){
Matrix m2 = new Matrix();
m2.createMatrix(zei, spa);
}
Notice that the Matrix m1 and m2 variables are declared after the first reference to them is made. Those declarations are also within the scope of the if block, which will cause them to be discarded as soon as the flow of execution exits the block and continues outside.
In other words, when the parser reaches the if (m1 == null) checks, it notices that the variables by the names of m1 and m2 do not exist yet.
You'll need to move the declarations outside of the body of the if statements so that you make sure they exist before you try to use them within a given scope. A quick search should provide good info on variable scoping rules for any language.
One possible way here is to turn your variables into private members of the MatrixMenu class, like so:
public class MatrixMenu {
private Matrix m1;
private Matrix m2;
// ...
if (m1 == null){
m1 = new Matrix();
m1.createMatrix(zei, spa);
}
else if (m2 == null){
m2 = new Matrix();
m2.createMatrix(zei, spa);
}
// ...
That's one possible way to solve your problem.
PS: Another point is that you should consider removing the createMatrix method and instead pass those arguments to the Matrix constructor to produce a valid/initialized matrix instance as soon as new returns. In other words:
public class Matrix {
private float[][] values;
public Matrix(int rows, int columns) {
// throw exception if any argument is <= 0
values = new float[rows][columns];
// ...
}
// ...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With