• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Populating 2D arrays

 
Mark Robertson
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please can anyone help- I am trying to write a program using JOptionPane which allows the user to input 5 numbers consecutively and then give them the option of repeating this 10 times. I would like to store these sets of numbers in a 2 dimensional array. Am I correct in thinking I need a nested 'for' loop for this?
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12015
24
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you probably COULD use a nesed for-loop, but it might not be the best idea. you say you want to let them OPTIONALLY repeat this ten times. a for-loop tends to be used to force something to be repeated X number of times. yes, you can do it, there are ways to break out, but they are generally not considered "best practice".

that being said, you might be putting the cart before the horse. can you write the code to let the user input ONE set of numbers? try and get that to work first, with a one dimensional array. then, try and get it to work with a 2-d array that is only 1 element wide (or tall, depending on how you want to think about it).

after you get all THAT to work, start thinking about how to get (up to) 10 sets of numbers.

it's much easier if you break the problem down into smaller, more managable pieces, THOROUGHLY test the code you write AT EACH NEW STEP, then continue.
 
Mark Robertson
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Fred, I have managed to achieve it with a 1 dimensional array using a single for loop. But have been stumped for days trying to upgrade it. Have been attempting to use the nessed for-loop as i was told that this was the only way to populate a two dimensional array but as you say it is not ideal if the user does not want 10 attempts. Also thought about the 'if' selector using an int variable such as count to loop around but this won't populate the 2D array will it? Do I have to populate each row of the 2d array individually? starting from one and finishing with 10- seems awful long winded. Apologies in advance for my naive understanding of the language-I am only a beginner and again, any help would be appreciated. Thanks, Mark
 
Joseph Sillitoe
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark,

Why don't you post the code that you have. Then we can all take a look and see if we can be helpful.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Say your current code looked like this:

How would you call loadOneRow() ten times?
[ January 10, 2006: Message edited by: Stan James ]
 
Layne Lund
Ranch Hand
Posts: 3061
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post what you have tried so far. Also describe the difficulties you have encountered. The more specific the questions you ask, the better we will be able to help you learn what is happening. And more importantly, we will be able to help you learn how to fix it in such a way that you will be able to fix it on your own in the future.

Layne
 
Mark Robertson
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone, this is what I have so far. It is obviously flawed but you might be able to see what I am trying to achieve. EG-

import javax.swing.JOptionPane;
import java.util.*;
public class Test {
public static void main(String[] args) {
int anotherGo;
String output = "";
String num1String;
int[][] Array1 = new int[10][5];
final int NUMBER_OF_GAMES = 10;
final int NUMBER_OF_BALLS = 5;

do {
for (int loop = 0; loop < NUMBER_OF_GAMES; loop++) {
for (int ball = 0; ball < NUMBER_OF_BALLS; ball++) {
num1String = JOptionPane.showInputDialog(
"please enter a number");
Array1[loop][ball] = Integer.parseInt(num1String);
}
}
anotherGo = JOptionPane.showConfirmDialog(null, output, "Arrays",JOptionPane.YES_NO_CANCEL_OPTION);
}while (anotherGo ==0);

System.exit(0);
}
}

After the user inputs 5 numbers, they should have an option to input another 5 and so on. The sets of numbers should be stored in the 2D array.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic