-3

I want to print an 2 dimensional double Array to the console.

  public class arrayprinter {
   public static void main(String[] args) {
        double[][] multi = new double[][]{
              { 10, 20, 30, 40, 50},
              { 1.1, 2.2, 3.3, 4.4},
              { 1.2, 3.2},
              { 1, 2, 3, 4, 5, 6, 7, 8, 9}
            };
        print(multi);
    }
    private static void print(double[][] e){
        for(int i=0; i>e.length;i++) {
            print(e[i]);
        }
    }
    public static void print(double[] e) {
        for(int i=0; i>e.length;i++) {
            System.out.print(e[i]);     }
    }
  }

When i click the play button in eclipse there is only: <terminated>...and no printed array in the console. Can someone say me what I am doing wrong?

Colin
  • 1,112
  • 1
  • 16
  • 27
  • 2
    Next times, just try to debug this. Print message here and there or use debugger. You would a found that the loop was wrong. – AxelH Dec 01 '16 at 13:21
  • The litlle bug icon did not found this. Maybe I have to have look at the debugger manual. – Colin Dec 01 '16 at 13:22
  • Yes you should read it. The debugger is not automatic, this is a runtime where you can add some break point. Those will stop the program when it read a specific line giving you the opportunities to check the value of your variable/instance. – AxelH Dec 01 '16 at 13:25
  • Bug icon? You're depending on the IDE too much. Thinking for yourself has to come first. – duffymo Dec 01 '16 at 13:26
  • There's no bug in this code from the IDE's point of view: it's valid Java, and it will compile and execute it dutifully. The bug is in the difference between what you intended and what you actually wrote, which the IDE can't possibly know. – Andy Turner Dec 01 '16 at 13:27
  • @Andy Turner It is a logical mistake no Syntax. I think bug is also logical. – Colin Dec 01 '16 at 13:29
  • @Colin right. But how can your IDE show a "bug icon" if you made a logical error? – Andy Turner Dec 01 '16 at 13:29
  • @Andy Turner It is the debug mode Icon and it has the shape of a bug. It is always there – Colin Dec 01 '16 at 13:32
  • @Colin you know that "debug" doesn't actually find bugs for you, right? *You* have to find them; it just helps you to do so. – Andy Turner Dec 01 '16 at 13:33
  • @Andy Turner but I could do line by line. But the Debugger rushed through the program code so I didn't know what was wrong. As I said I have to have a look at the debugger manual next time. – Colin Dec 01 '16 at 13:35

4 Answers4

6

You want to do

i < e.length

not "i>e.length".

Andrea
  • 6,032
  • 2
  • 28
  • 55
2

Your program never reaches the line which prints. You start at i=0 and increment it while it's bigger that e.length, but it never is, because the loop exists at the first run (i = 0 and 0 is not bigger than e.length so it exists the loop)
use these:

private static void print(double[][] e) {
    for (int i = 0; i < e.length; i++) {
        print(e[i]);
    }
}

public static void print(double[] e) {
    for (int i = 0; i < e.length; i++) {
        System.out.print(e[i]);
    }
}

I replaced the > with < in the loop.

ItamarG3
  • 4,092
  • 6
  • 31
  • 44
1

Your comparison i>e.length is wrong it should be i < e.length, see code below

private static void print(double[][] e){
    for(int i=0; i<e.length;i++) {
        print(e[i]);
    }
}
public static void print(double[] e) {
    for(int i=0; i<e.length;i++) {
        System.out.print(e[i]);     }
}
baliman
  • 588
  • 2
  • 8
  • 27
0

Instead of writing two methods you can also use a one liner like

public static void main(String[] args) {
       double[][] multi = new double[][]{
          { 10, 20, 30, 40, 50},
          { 1.1, 2.2, 3.3, 4.4},
          { 1.2, 3.2},
          { 1, 2, 3, 4, 5, 6, 7, 8, 9}
        };
        System.out.println(Arrays.deepToString(multi));
    }
Eritrean
  • 15,851
  • 3
  • 22
  • 28