1

I need to print this in the correct two dimensional array format. SOmething is wrong. Need the print from the method. My output is what seems to be an infinite loop.

import java.util.Scanner;
public class hw3 {
public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("What is the dimension of your matrix?");
    int matrixdim = input.nextInt();
    double[][] matrix = new double[matrixdim][matrixdim];

    System.out.println("Enter " + matrixdim + " rows, and " + matrixdim + " columns." );
    Scanner input1= new Scanner(System.in);
    for (int row = 0; row < matrix.length; row++) {
        for (int column = 0; column < matrix[row].length; column++)
            matrix[row][column] = input1.nextDouble();
    }
    System.out.println("Your original array:");

    System.out.println(printArray(matrix));
}   
public static double printArray(double matrix[][]){
    for (int row = 0; row < matrix.length; row++) {
        for (int column = 0; column < matrix[row].length;column++) {
        System.out.println(matrix[row][column] + " ");
    }
    System.out.println();
}
return printArray(matrix);
user2484149
  • 31
  • 1
  • 2
  • 5
  • What exactly is happening when you run the code? – John Jun 14 '13 at 23:27
  • 1
    If you are having an infinite loop, you must be missing a `}`...Check for the `{}` consistency. If you are using some IDE, like Eclipse, just right click on the mouse --> Source --> Format. This should help you with correct indentation. – CHEBURASHKA Jun 14 '13 at 23:31

4 Answers4

12

As I told you in my previous answer invoking same method again return printArray(matrix); at the end of your method can lead to invoking it again (and again) until StackOverflow error.

Change return type to void. Now your method can look like

public static void printArray(double matrix[][]) {
    for (int row = 0; row < matrix.length; row++) {
        for (int column = 0; column < matrix[row].length; column++) {
            System.out.print(matrix[row][column] + " ");
        }
        System.out.println();
    }
}

or even better

public static void printArray(double matrix[][]) {
    for (double[] row : matrix) 
        System.out.println(Arrays.toString(row));       
}
Community
  • 1
  • 1
Pshemo
  • 122,468
  • 25
  • 185
  • 269
2

Just change the println to print in the first print call.

public static void printArray(double matrix[][]){
    for (...) {
        for (...) {
            //here just print goes
            System.out.print(matrix[row][column] + " ");
        }
        //at the end each row of the matrix you want the new line - println is good here
        System.out.println();
    }
}

print does not print the newline (\n) at the end of the output, whereas the println does. That's why you're getting the ugly print.

Also, printArray should not return a value, it should be:

public static void printArray(double[][] matrix)

I think that's where you're getting your infinite loop. Don't return anything - no need, you are just printing it.

jahroy
  • 22,322
  • 9
  • 59
  • 108
darijan
  • 9,725
  • 25
  • 38
  • @jahroy Because it's a recursive call. Every time method tries to return a value it calls itself again. Try this method: `public int someMethod(int i) { return someMethod(i); }` and call it wherever from. – darijan Jun 14 '13 at 23:49
  • Yes... I understand that. I mis-read the code at first due to its poor formatting and the missing `}`. That said, the return type doesn't cause the infinite loop... it's the fact that the method invokes itself. I have removed my downvote. – jahroy Jun 14 '13 at 23:51
1

You are missing a } and use print instead of println inside your 2nd loop.

public static double printArray(double matrix[][])
{
    for (int row = 0; row < matrix.length; row++) 
    {
        for (int column = 0; column < matrix[row].length;column++) 
        {
            System.out.print(matrix[row][column] + " ");
        }
        System.out.println();
    }
}
Simon Arsenault
  • 1,777
  • 17
  • 35
-1

you got System.out.println(printArray(matrix)); instead of printArray(matrix); since your method got print calls in it anyway

and as mentioned above - print vs println

Artur Dębski
  • 34
  • 1
  • 3