Joel
Joel

Reputation: 2721

Inserting string array value in for loop in java

I know this is a very simple thing, but I can't see any examples of doing this with strings. This is beyond the basic exercise in my self imposed homework, and more advanced, but I know it can be done so I just want to go ahead and learn these arrays :-D

I'm trying to change the value if the string in the GLabel below:

private void printSubclassBoxes(){
        String[] anArray = {"GraphicsProgram", "ConsoleProgram", "DialogProgram"};

        int  coordinateX = ((getWidth() - BOX_WIDTH) /4);
        int otherCoordinateX = coordinateX;

        for ( int i = 0 ; i < 3; i++){

            double coordinateY = (getHeight() / 2);     
            GRect classBox = new GRect (coordinateX, coordinateY, BOX_WIDTH, BOX_HEIGHT);

            GLabel classLabel = new GLabel ("ARRAY WILL GO HERE");
            double labelCoordinateX = (coordinateX + ((classBox.getWidth() / 2) - (classLabel.getWidth() / 2)));
            double labelCoordinateY = (coordinateY + ((classBox.getHeight() / 2) + (classLabel.getAscent() / 2)));
            add(classBox);
            add(classLabel, labelCoordinateX, labelCoordinateY);
            coordinateX = otherCoordinateX + coordinateX;

        }   

    }

Thanks for the help!

Upvotes: 1

Views: 937

Answers (1)

Stephen C
Stephen C

Reputation: 718758

I think you want this:

GLabel classLabel = new GLabel (anArray[i]);

Upvotes: 2

Related Questions