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 + "]";


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);


Thanks for any help

EDIT: Note that the output from the program should be:

[0 0 0]
[0 0 0]
[0 0 0]

Recommended Answers

All 4 Replies

ehm ... I just tested that code here, and it works perfectly. one remark though, if you want to print those lines below each other, instead of next to each other, you'll need to change your last mutation of the output value to:

output = output + "] \n";

did you save and recompile before you ran it?

Hmm.. that's weird. I mean, it should be working, I'm just confused as to why it isn't working for me. I just recompiled, and still the same thing is happening. It's weird because I had almost identical code when I had the printing process in the main class, and it worked perfectly, but as soon as I moved it into the separate Matrix file, it came up with that weird output.

Thanks for the help, and I will put that newline thing in. I guess I'll have to test it more thoroughly on my end, but it's good to know that the code does actually work somewhere.

I had the main in a different class as well. but even when not, I don't see any reason why that should give a problem.

maybe you can add a few print statements in your code, to check on sub-parts. if you don't see those appearing in your console, you know your code hasn't been updated, or isn't properly running.

Okay, well I just tried copying and pasting the code into a new project, and it works perfectly (I don't know why I didn't try that before). It must've been something wrong with my IDE. Thanks anyways!

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.