# User Input and Array Length

Page 1 of 1

## 10 Replies - 513 Views - Last Post: 24 July 2013 - 07:43 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=325499&amp;s=898b43d58f84b3f21ab97d2d5dfdd3b8&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Freshman

Reputation: 0
• Posts: 6
• Joined: 23-July 13

# User Input and Array Length

Posted 23 July 2013 - 10:55 PM

What should I do to make the user input the length of my 2D array?
Example:
Enter number: 3
Then the length of my 2d array will be 3.
Is This A Good Question/Topic? 0

## Replies To: User Input and Array Length

### #2 Flukeshot

• A little too OCD

Reputation: 417
• Posts: 1,030
• Joined: 14-November 12

## Re: User Input and Array Length

Posted 23 July 2013 - 11:06 PM

Which dimension will be 3 indexes in length?

A 2D array is basically an array of arrays. Arrays are not flexible, so you'll want to define both dimensions and stick to it.

The length of the first dimension is defined by "How many arrays do you want?" while the second dimension is defined by "How many elements are in each array?"

Note: the lengths of the second dimension are free to differ per array. The first array could be 3 indexes in length, the second array could be 4 indexes, and the third could be 7 indexes long. That gives you an of 3 arrays containing 3 elements in array[0], 4 elements in array[1] and 7 elements in array[2].

### #3 Freshman

Reputation: 0
• Posts: 6
• Joined: 23-July 13

## Re: User Input and Array Length

Posted 24 July 2013 - 01:12 AM

They both must have 3 indexes. Thanks for the help, I found out the answer. Now I just want to ask if how am I suppose to eliminate
[0][0],[1][1],[2][2] etc.because they have an automatic answer of "N".
All I need to print is [0][1],[0][2],[1][2]
Like this:
Is Family(0) related to Family(1)?[Y/N]: Y
Is Family(0) related to Family(2)?[Y/N]: Y
Is Family(1) related to Family(2)?[Y/N]: Y

Sorry for wrong usage of word. It will skip not terminate this indexes [0][0],[1][1],[2][2] because they already have an automatic answer of "N" that's why I don't need to ask question like this:
Is Family(0) related to Family(0)?[Y/N]:
Is Family(1) related to Family(1)?[Y/N]:
Is Family(2) related to Family(2)?[Y/N]:

### #4 Flukeshot

• A little too OCD

Reputation: 417
• Posts: 1,030
• Joined: 14-November 12

## Re: User Input and Array Length

Posted 24 July 2013 - 01:14 AM

I suppose you'd be using a nested loop to populate the array? So, if(i == j) continue; or something.

### #5 Freshman

Reputation: 0
• Posts: 6
• Joined: 23-July 13

## Re: User Input and Array Length

Posted 24 July 2013 - 01:19 AM

Yes a for loop inside a for loop.
This is my code:

```System.out.print("Is Family("+i+")and Family("+j+") related?[Y/N]: ");
isFriend[i][j] = userInput.next();
if (isFriend[i] == (isFriend[j])){
isFriend[i][j] = "N";
}

```

That is the condition inside the second for loop. i tried to skip but it didn't work

### #6 Flukeshot

• A little too OCD

Reputation: 417
• Posts: 1,030
• Joined: 14-November 12

## Re: User Input and Array Length

Posted 24 July 2013 - 01:50 AM

You aren't supposed to confirm between the separate dimensions of the array, only the iterations of the loop.

It would be described somewhat like this:
```for i
for j
if i==j: continue
else: assign the value
```

### #7 Freshman

Reputation: 0
• Posts: 6
• Joined: 23-July 13

## Re: User Input and Array Length

Posted 24 July 2013 - 02:01 AM

Sir/Ma'am thanks for the help it works. I will be back for more questions. Thanks again

### #8 Freshman

Reputation: 0
• Posts: 6
• Joined: 23-July 13

## Re: User Input and Array Length

Posted 24 July 2013 - 02:14 AM

This is my question. I want also to skip the reverse. For example:
```Is Family(0) related to Family(1)?[Y/N]:
Is Family(0) related to Family(2)?[Y/N]:
Is Family(1) related to Family(0)?[Y/N]:
Is Family(1) related to Family(2)?[Y/N]:
Is Family(2) related to Family(0)?[Y/N]:
Is Family(2) related to Family(1)?[Y/N]:

```

I want to skip the [1][0],[2][0], and [2][1] because their reversed part already has an answer?

Should I put it inside the first If statement?

### #9 Flukeshot

• A little too OCD

Reputation: 417
• Posts: 1,030
• Joined: 14-November 12

## Re: User Input and Array Length

Posted 24 July 2013 - 02:24 AM

Use 2 assignments per loop:

assign array[i][j] and array[j][i] the same value.

When you ask for input, check that the location you are entering into is not null.

This post has been edited by Flukeshot: 24 July 2013 - 02:25 AM

### #10 Freshman

Reputation: 0
• Posts: 6
• Joined: 23-July 13

## Re: User Input and Array Length

Posted 24 July 2013 - 03:11 AM

```if (isFriend[i][j] != null){
continue;
}
else{
isFriend[j][i] = isFriend[i][j];
}

```

that's what my code is

### #11 Flukeshot

• A little too OCD

Reputation: 417
• Posts: 1,030
• Joined: 14-November 12

## Re: User Input and Array Length

Posted 24 July 2013 - 07:43 AM

Slight error on the first conditional.
```if(isFriend[i][j]!=null)

```

should be:
```if(isFriend[j][i]!=null)

```