wood burning stoves
The moose likes Beginning Java and the fly likes Populating 2D arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Populating 2D arrays" Watch "Populating 2D arrays" New topic

Populating 2D arrays

Mark Robertson

Joined: Jan 10, 2006
Posts: 11
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

Joined: Oct 02, 2003
Posts: 11951

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.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Mark Robertson

Joined: Jan 10, 2006
Posts: 11
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

Joined: Dec 30, 2005
Posts: 12

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

Joined: Jan 29, 2003
Posts: 8791
Say your current code looked like this:

How would you call loadOneRow() ten times?
[ January 10, 2006: Message edited by: Stan James ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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.


Java API Documentation
The Java Tutorial
Mark Robertson

Joined: Jan 10, 2006
Posts: 11
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);


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.
I agree. Here's the link: http://aspose.com/file-tools
subject: Populating 2D arrays
It's not a secret anymore!