0

I have an array:

 [ 0  0
   1  0
   2  0
   3  0
   4  0
   5  0 ]

How do I specify where the user input (using scanner) will go to in the array? I would like to add an integer to each position of the second column.

1
  • try something before asking question. Commented Nov 15, 2012 at 10:40

1 Answer 1

1

For an array of array, to get the number of rows, you use the length of your array.

For e.g, for this array: -

int[][] arr = new int[3][4];

arr.length gives you number of rows in the array, i.e. 3. So, run a loop from 0 to arr.length, to access each row.

Now, to access the 2nd column of each row, you can do arr[i][1] in your loop: -

for (int i = 0; i < arr.length; i++) {
    arr[i][1] = ...;  // your 2nd column for each row
}

Also, to get user input to fill your 2nd column for each row, you have to read input for each row. So, you can guess where you need to read the user input - In outer loop, or in inner loop?

Sign up to request clarification or add additional context in comments.

7 Comments

what does the 1 in arr[i][1] represent?
@Kronos.. i is the variable in your outer loop. arr[i][1] will be arr[0][1], arr[1][1], ... so on. for each row.
@Kronos.. Common. at least write some code. Come up with what you tried. Then you can get more help. I'm not going to just give away the code. You will not learn anything that way.
I'm sorry, I meant 1. When I have something to show you I will. At the moment I have no clue where to start for this problem, that is why I am asking for help.
1 is the index of 2nd column. arr[0][1] means 1st row 2nd column.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.