Okay, I'm very new to Java as a whole, and I'm just trying to get to grips with it. I'm experimenting with classes, and I'm struggling to get this one thing to work.
Basically, I've made a short program which one can make matrices, and all it has at the moment is a function to print them out and edit their entries. However, although I managed to get the print function to work when it was in my main java file, when I move the print function to the class I made: Matrix, for some reason the printing horribly fails -- and goes into a sort of infinite loop. Well, basically the output ends up being "0 " then a new line with only " ", or something like that, and this output is repeated indefinitely.
I just hope that someone can take a quick look at this, and see if there is anything obviously wrong with it that my eyes aren't quite sharp enough to see yet.
Here is the code for the file containing information about the class Matrix:
public class Matrix {
public int[][] matrixEntries;
public int rowSize, columnSize;
public Matrix(int chosenRowSize, int chosenColumnSize) {
matrixEntries = new int[chosenRowSize][chosenColumnSize];
rowSize = chosenRowSize;
columnSize = chosenColumnSize;
}
public void changeEntry(int rowNumber, int columnNumber, int newEntry) {
matrixEntries[(rowNumber-1)][(columnNumber-1)] = newEntry;
}
public void print() {
String output = "";
for (int j = 0; j < columnSize; j++) {
output = output + "[";
for (int i = 0; i < rowSize; i++) {
output = output + matrixEntries[i][j];
if (!(i == rowSize - 1)) {
output = output + " ";
}
}
output = output + "]";
}
System.out.println(output);
}
}
And here is the code for the actual file which will be executed
public class main_File {
public static void main(String[] args) {
Matrix awesomeMatrix = new Matrix(3,3);
awesomeMatrix.print();
}
}
Thanks for any help
EDIT: Note that the output from the program should be:
[0 0 0]
[0 0 0]
[0 0 0]