# 2 dimensional array coding

• July 22nd, 2011, 12:55 PM
Bighairjersey
2 dimensional array coding
I have been trying to learn java coding from a book and have run into a coding question that I'm confused about. Can someone help?
I have a one dimensional array of positive integers. I'm trying to create another array with the same length and scan the original array for finding the maximum integer. I am then needing to copy that maximum integer in first position of the created array and set the maximum integer in the original array to zero. For second round, I need to write code to find the maximum integer in the original array and copy that in second position of created array and set the maximum integer in the original array to zero. I need to repeat this until all numbers in original array is zero. My created array should be sorted in positive integer numbers. I need to write this sort method that accepts a one dimensional array of integers that return the sorted array testing this method using the main method.
• July 22nd, 2011, 01:09 PM
Norm
Re: 2 dimensional array coding
Can you post your code with questions about the problems you are having?

Where is the 2 dimensional array in all this?
• July 22nd, 2011, 01:18 PM
Bighairjersey
Re: 2 dimensional array coding
The book that I'm going through provides questions and for the user to write the code. Part of my problem is that I don't know where to start, confused as to what it is really asking for. Just trying to learn java on the side so I have no really knowledge or training.
• July 22nd, 2011, 01:23 PM
Norm
Re: 2 dimensional array coding
Quote:

Just trying to learn java
That's what books are for. Read about a technique and then key the examples and change them this way and that way to see what happens.
Does the book provide any examples you can type in?
• July 22nd, 2011, 01:30 PM
Bighairjersey
Re: 2 dimensional array coding
Yes, I went through those however, I'm going through the examples at the back and the book doesn't give these solutions. I wanted to see how the coding for this was to see if it made sense.
• July 22nd, 2011, 01:38 PM
Norm
Re: 2 dimensional array coding
As I suggested before, type in some examples from the book and make changes to them and see what happens.
• July 22nd, 2011, 02:12 PM
Bighairjersey
Re: 2 dimensional array coding
Ok, could you help with this one? I want to Write a program that asks user for the two dimensions of a two dimensional integer matrix and then the program keep asking all elements of the matrix by row and column from user and then printing the content of the matrix at the end.
This is what I have so far. The print coding is in correct, not sure how to print what the person enters.

Scanner input = new Scanner(System.in);

int[][] m = new int[3][4];
System.out.println("Enter 2 dimensions" + m.length);

for (int i = 0; i < m.length; i++);
int i = 0;
for (int j = 0; j < m[i].length; j++)m [i][j] = input.nextInt();

System.out.print([i][j]);
• July 22nd, 2011, 02:21 PM
Norm
Re: 2 dimensional array coding
Quote:

how to print what the person enters.
First you need to read what the user enters.
Read the API doc for the Scanner class. It has methods to read input from the user.
Also search here on the forum for Scanner examples.
Once you get the what was entered into a variable, you'd use the println() method call to print it.
• July 22nd, 2011, 02:30 PM
Norm
Re: 2 dimensional array coding
```Scanner input = new Scanner(System.in);   int[][] m = new int[3][4]; System.out.println("Enter 2 dimensions" + m.length);   for (int i = 0; i < m.length; i++) { // removed ; here and added { // remove this line, i is defined above int i = 0; for (int j = 0; j < m[i].length; j++) { m [i][j] = input.nextInt(); // get next input from user System.out.print(m[i][j]); // show what the user entered } // end for(j) } // end for(i)```