This site requires JavaScript, please enable it in your browser!
Greenfoot back
decadence18
decadence18 wrote ...

2015/3/28

How can I dynamically add a certain number of "variables"?

decadence18 decadence18

2015/3/28

#
What my INTENTION is, is to have a user input an integer, then according to that integer, a set of variables or array or list or something created containing the user's input for each of the items. Example: The user inputs the number 6, so a loop is created asking the user for a number 6 times, and then each answer is inserted into an array. The next time, the user inputs the number 7, so this time, the user is prompted 7 times. How would I accomplish this?
fejfo fejfo

2015/3/28

#
u would have to use an arraylist
decadence18 decadence18

2015/3/28

#
fejfo wrote...
u would have to use an arraylist
Thank you, but I assumed I would have to use some sort of Array or List or ArrayList or something. I'm still working on learning Java, thus I am not familiar with how one would be able to go about such a task.
danpost danpost

2015/3/28

#
What is the range of answer values that could be input? A little background information can go a long way.
decadence18 decadence18

2015/3/28

#
@danpost What I'm attempting to do is paint a number of named images (Image 1.png, Image 2.png, etc) in the order the user specifies. I have the code now to where a user can input a number (1, 2, or 3) and it will paint the images in the order they specified. However, it can only be used to paint 3 images. What I'm trying to do now is to have it dynamically generate a set of "variables" (a term used loosely for anything that can contain data), with this set generated to however many variables the user specifies (such as, if the user input 6 for the question "How many boards are there?", he would then be prompted for the order of said 6 boards).
danpost danpost

2015/3/28

#
Instead of having the initial number of items to enter entered first, you could just continue to have numbers input for the items until a zero is entered or something to signify done and build the main image with each valid number entered before zero is entered. You could use any type loop and just 'break' out of it when zero is entered.
decadence18 decadence18

2015/3/28

#
True, I didn't consider that. I'll try that Danpost and reply back on here again if I can't get it to work
decadence18 decadence18

2015/3/28

#
danpost wrote...
Instead of having the initial number of items to enter entered first, you could just continue to have numbers input for the items until a zero is entered or something to signify done and build the main image with each valid number entered before zero is entered. You could use any type loop and just 'break' out of it when zero is entered.
decadence18 wrote...
True, I didn't consider that. I'll try that Danpost and reply back on here again if I can't get it to work
Okay so, I have no idea where to go from here.
boardPositions = JOptionPane.showInputDialog("Enter the order of the boards, separated by a space.\nExample: 1 2 2 1");
        String[] boards = boardPositions.split(" ");
        
        if ((boardPositions != null) && (boardPositions.isEmpty() != true))
        {
            for(int i=0; i<boards.length; i++)
            {
                GreenfootImage[] images =
                {
                    new GreenfootImage("Horizontal Hexagon Board (33x10) " + boards[i] + ".png")
                };
                int x = 0; // starting x-coordinate for drawing images
                for (int n=0; n<images.length; n++) // for each image
                { 
                    getBackground().drawImage(images[n], x, 0); // draw image on background
                    x += images[n].getWidth(); // set new x-coordinate for next image draw
                }
            }
        }
I'm attempting to add a new image into the "images" array for each value input into the original "boards" array, so that it will paint each image onto the background. I've tried almost every way I can think of doing it (I'm more versed in JavaScript than Java, which is where most of my knowledge of arrays and loops come from). Any ideas?
decadence18 decadence18

2015/3/28

#
Nevermind. I got it.
boardPositions = JOptionPane.showInputDialog("Enter the order of the boards, separated by a space.\nExample: 1 2 2 1");
        String[] boards = boardPositions.split(" ");
        
        if ((boardPositions != null) && (boardPositions.isEmpty() != true))
        {
            int x = 0;
            for(int i=0; i<boards.length; i++)
            {
                GreenfootImage image = new GreenfootImage("Horizontal Hexagon Board (33x10) " + boards[i] + ".png");
                getBackground().drawImage(image, x, 0); // draw image on background
                x += image.getWidth(); // set new x-coordinate for next image draw
            }
        }
By leaving the "int x = 0" in the for loop it kept resetting to 0. It now works correctly. Thank you @danpost
danpost danpost

2015/3/29

#
I was thinking more along these lines:
int boardNum = 0;
int xPos = 0;
for (;;)
{
    String caption = "Board "+(char)(65+boardNum)+":  select ";
    caption += "board number (1 - 4) or enter '0' to discontinue.";
    String input = javax.swing.JOptionPane.showInputDialog(caption);
    if (input == null || input.isEmpty() || "0".equals(input)) break;
    if (input.length() != 1 || "1234".indexOf(input) == -1) continue;
    String filename = "Horizontal Hexagon Board (33x10) "+input+".png";
    GreenfootImage image = new GreenfootImage(filename);
    getBackground().drawImage(image, xPos, 0);
    xPos += image.getWidth();
    boardNum++;
}
decadence18 decadence18

2015/3/29

#
That might work too. I have two classes that need to use this painting technique; I may try both to see which one would work better for this purpose.
You need to login to post a reply.