0

I was trying to do a 2D array program to demonstrate a TRANSPOSE but I am getting error .. here is my code.

import java.util.Scanner;

/* To demonstrate TRANSPOSE USING 2-D array */
public class Array_2ddd {
    public static void main(String args[]) {
        Scanner s1 = new Scanner(System.in);
        int i, j;
        int myArray1[][] = new int[9][9];
        int myArray2[][] = new int[9][9];
        for (i = 0; i < 9; i++) {
            for (j = 0; j < 9; j++) {
                System.out.println("Enter array from 1 to 9");
                myArray1[i][j] = s1.nextInt();
                System.out.print("your array is" + myArray2[i][j]);
            }
        }
        // Transposing now...
        for (i = 0; i < 9; i++) {
            for (j = 0; j < 9; j++) {
                myArray2[i][j] = myArray1[j][i];
            }
        }
        // After transposing
        for (i = 0; i < 9; i++) {
            for (j = 0; j < 9; j++) {
                System.out.print("Your array is as follow" + myArray2[i][j]);
            }
        }
    }
}
  • EDIT: My error during runtime (Solved)
  • EDIT 2: Solved
  • EDIT 3: The loop is in infinity ..it keeps on asking for values fromt the user even when i wrote i<9 and j<9..it still keeps on asking for values till infinity..
Rob
  • 14,746
  • 28
  • 47
  • 65
Smit Shah
  • 41
  • 7
  • 5
    Can you post the error? – Thiyagu May 15 '18 at 16:45
  • 1
    Array indices start at 0. `for (i = 0; i < 9; i++)` – Thiyagu May 15 '18 at 16:46
  • Also you're missing a bracket in your inner for loop – GBlodgett May 15 '18 at 16:46
  • 1
    What kind of error? Compilation error or runtime error. Please post the full error message including the stack trace for runtime error. – vanje May 15 '18 at 16:49
  • 1
    Possible duplicate of [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](https://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – 4castle May 15 '18 at 16:52
  • 1
    Welcome to Stack Overflow! To ensure your success on this site, please read and apply the information in the [tour] and the [ask] page. – 4castle May 15 '18 at 16:56
  • Possible duplicate of [What's the simplest way to print a Java array?](//stackoverflow.com/q/409784) – 4castle May 15 '18 at 17:01
  • Look at the output guys..I added a Output pic in the question itself. – Smit Shah May 15 '18 at 17:01

3 Answers3

0

You define your matrix as 9x9

    int myArray1[][] = new int[9][9];

But actually you want to insert 10x10 items:

 for (i = 0; i <= 9; i++)
    {
        for (j = 0; j <= 9; j++)

So either:

  1. Redefine your arrays to store 10x10 items

    int myArray1[][] = new int[10][10];

  2. Only read and store 9x9 items in your defined array

    for (i = 0; i < 9; i++) {
        for (j = 0; j < 9; j++)
    
Mạnh Quyết Nguyễn
  • 17,677
  • 1
  • 23
  • 51
  • Hey thanks for the reply..now i after making changes i got some weird output. Your array is as follow[[I@6ea12c19Your array is as follow[[I@6ea12c19Your array is as follow[[I@6ea12c19.........Your array is as follow[[I@6ea12c19Your array is as follow[[I@6ea12c1 Process finished with exit code 0 – Smit Shah May 15 '18 at 16:55
  • You're printing `myArray2` which is a pointer/reference array. You must read from array as you write to it: `System.out.print("Your array is as follow" + myArray2[i][j]);` – Mạnh Quyết Nguyễn May 15 '18 at 16:57
0

You haven't close your first outer for loop i.e in line 17 and change your array size to 10,as you wanted take 10 input (for 0 to 9 = 10 values).

0

There are several errors in your code, also it is recommend that the dimensions of the array is to be declared as a final int, so your code works for all matrix sizes and that debugging is easier. In your original code, the errors are:

  • At the input step, you are printing one element of myArray[2] before you perform the transpose. That means, you are getting your array is0.
  • In the section commented "After transposing", you are outputting your array wrong. Namely, for each entry, you call System.out.print("Your array is as follow" + myArray2[i][j]);, and that you forgot to add a new line after each row (when inner loop is finished).
  • "..it keeps on asking for values fromt the user even when i wrote i<9 and j<9..it still keeps on asking for values till infinity.." There are 81 entries for the 9-by-9 case and you did not output which i,j index to be applied. You probably mistaken an infinite loop with a long but terminating loop.

Your transpose step is good.

Here is a refined version of your code which allows you to input array (in reading order, or more technically, row-major order), create a transposed array. You can copy and compare your current code with this code directly to test it.

public static void main(String args[]) {
    final int m = 9; // Rows
    final int n = 9; // Columns
    Scanner s1 = new Scanner(System.in);
    int i, j;

    int myArray1[][] = new int[m][n]; // Original array, m rows n cols
    int myArray2[][] = new int[n][m]; // Transposed array, n rows m cols

    // Input
    for (i = 0; i < m; i++) {
        for (j = 0; j < n; j++) {
            // Should be only prompt.
            // Improved to show which entry will be affected.
            System.out.printf("[%d][%d]" + "Enter array from 1 to 9\n", i, j); 
            myArray1[i][j] = s1.nextInt();
        }
    }

    // Transposing now (watch for the ordering of m, n in loops)...
    for (i = 0; i < n; i++)
    {
        for (j = 0; j < m; j++)
        {
            myArray2[i][j] = myArray1[j][i];
        }
    }

    // After transposing, output
    System.out.print("Your array is:\n");
    for (i = 0; i < m; i++) {
        for (j = 0; j < n; j++) {
            System.out.print(myArray1[i][j] + " ");
        }
        System.out.println(); // New line after row is finished
    }

    System.out.print("Your transposed array is:\n");
    for (i = 0; i < n; i++) {
        for (j = 0; j < m; j++) {
            System.out.print(myArray2[i][j] + " ");
        }
        System.out.println();
    }
    s1.close();
}

For an array with three rows (m = 3) and four columns (n = 4), I inputted the numbers from 0 to 9, and then 0, 1, 2. As expected, the output should be:

Your array is:
0 1 2 3 
4 5 6 7 
8 9 0 1 
Your transposed array is:
0 4 8 
1 5 9 
2 6 0 
3 7 1