10 Replies - 269 Views - Last Post: 24 July 2013 - 07:43 AM Rate Topic: -----

#1 Freshman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 415
  • View blog
  • 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].
Was This Post Helpful? 1
  • +
  • -

#3 Freshman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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]:
Was This Post Helpful? 0
  • +
  • -

#4 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 415
  • View blog
  • 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. :)
Was This Post Helpful? 0
  • +
  • -

#5 Freshman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#6 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 415
  • View blog
  • 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

Was This Post Helpful? 1
  • +
  • -

#7 Freshman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#8 Freshman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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:
I already skipped the [0][0],[1][1],[2][2]
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?
Was This Post Helpful? 0
  • +
  • -

#9 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 415
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#10 Freshman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#11 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 415
  • View blog
  • 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)


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1