Question

Ok so I'm building to show students how a loop goes through an array, I have added 2 images to help explain and the code, the first is the result I get after I click go then it freezes . The Second image is what I'd like it to do after you put in the values of 1 in start, 15 in stop, 3 in step and click the Go Button. And then to be cleared on the click of Clear button. I think they probably related. Can anyone see the problem? Thanks in advanced!

app after i click go (frozen)

the result id like

 import java.awt.*;
 import java.awt.event.*;
 import java.awt.Color;
 import javax.swing.JOptionPane;

 public class Checkerboard extends Frame implements ActionListener
 {
int[] blocksTextField = new int[15];

Panel blocksPanel = new Panel();
TextArea blocksDisplay[] = new TextArea[16];

TextField start = new TextField (3);
TextField stop = new TextField (3);
TextField step = new TextField (3);

//Colors
Color Red = new Color(255, 90, 90);
Color Green = new Color(140, 215, 40);
Color white = new Color(255,255,255);

//textField ints
int inputStart;
int inputStop;
int inputStep;

//Lables
Label custStartLabel = new Label ("Start : ");
Label custStopLabel = new Label ("Stop : ");
Label custStepLabel = new Label ("Step : ");

//Buttons
Button goButton = new Button("Go");
Button clearButton = new Button("Clear");

//panel for input textFields and lables
Panel textInputPanel = new Panel();

//Panel for buttons
Panel buttonPanel = new Panel();

public Checkerboard()
{//constructor method

    //set the 3 input textFields to 0
    inputStart = 0;
    inputStop = 0;
    inputStep = 0;

    //set Layouts for frame and three panels
    this.setLayout(new BorderLayout());
                       //grid layout   (row,col,horgap,vertgap)
    blocksPanel.setLayout(new GridLayout(4,4,10,10));

    textInputPanel.setLayout(new GridLayout(2,3,20,10));

    buttonPanel.setLayout(new FlowLayout());

    //setEditable()
    //setText()

    //add components to blocks panel
    for (int i = 0; i<16; i++)
    {
        blocksDisplay[i] = new TextArea(null,3,5,3);
        if(i<6)
            blocksDisplay[i].setText(" " +i);
        else
            blocksDisplay[i].setText(" " +i);
            blocksDisplay[i].setEditable(false);
        //  blocksDisplay[i].setBackground(Red);
            blocksPanel.add(blocksDisplay[i]);
    }//end for

    //add componets to panels
    //add text fields
    textInputPanel.add(start);
    textInputPanel.add(stop);
    textInputPanel.add(step);
    //add lables
    textInputPanel.add(custStartLabel);
    textInputPanel.add(custStopLabel);
    textInputPanel.add(custStepLabel);
    //add button to panel

    buttonPanel.add(goButton);
    buttonPanel.add(clearButton);

    //ADD ACTION LISTENRS TO BUTTONS (!IMPORTANT)
    goButton.addActionListener(this);
    clearButton.addActionListener(this);

     add(blocksPanel, BorderLayout.NORTH);
    add(textInputPanel, BorderLayout.CENTER);
    add(buttonPanel, BorderLayout.SOUTH);

    //overridding the windowcClosing() method will allow the user to clisk the Close button
            addWindowListener(
                new WindowAdapter()
                {
                    public void windowCloseing(WindowEvent e)
                    {
                        System.exit(0);
                    }
                }
    );

}//end of constructor method

public void actionPerformed(ActionEvent e)
{

    //if & else if to see what button clicked and pull user input
    if(e.getSource() == goButton) //if go clicked ...
        {
        System.out.println("go clicked");

    try{
            String inputStart = start.getText();
            int varStart = Integer.parseInt(inputStart);
            if (varStart<=0 || varStart>=15 )throw new NumberFormatException();

            System.out.println("start = " + varStart);
            //  roomDisplay[available].setBackground(lightRed);

            String inputStop = stop.getText();
            int varStop = Integer.parseInt(inputStop);
            if (varStop<=0 || varStart>=15 )throw new NumberFormatException();
            System.out.println("stop = " + varStop);

            String inputStep = step.getText();
            int varStep = Integer.parseInt(inputStep);
            if (varStep<=0 || varStep>=15 )throw new NumberFormatException();
            System.out.println("step = " + varStep);

                for (int i = varStart; i<varStop; varStep++)//ADD WHILE LOOP
                        {
                            blocksDisplay[i].setBackground(Red);
                            blocksDisplay[i].setText(" " +i);
                        }

        }

    catch (NumberFormatException ex)
        {
            JOptionPane.showMessageDialog(null, "You must enter a Start, Stop and Step value greater than 0 and less than 15",
            "Error",JOptionPane.ERROR_MESSAGE);
        }

        }
        else if(e.getSource() == clearButton ) //else if clear clicked ...
        {
        System.out.println("clear clicked");
        }

    //int available = room.bookRoom(smoking.getState());
    //if (available > 0)//Rooms is available

}//end action performed method

public static void main(String[]args)
{
    Checkerboard frame = new Checkerboard ();
        frame.setBounds(50, 100, 300, 410);//changed  size to make text feilds full charater size
        frame.setTitle("Checkerboarder Array");
        frame.setVisible(true);
}//end of main method


 }
Was it helpful?

Solution 2

Take a look at this loop.

   for (int i = varStart; i<varStop; varStep++)//ADD WHILE LOOP
   {
        blocksDisplay[i].setBackground(Red);
        blocksDisplay[i].setText(" " +i);
   }

It ends when i >= varStop, but neither i nor varStop change as a consequence of its execution, so it can never stop. You only increment varStep.

I think you want to increment i by varStep on each iteration instead, i.e. i += varStep

OTHER TIPS

The problem is your loop: your loop variable name is i but you change the varStep variable instead of i so basically the loop variable never changes and thus the exit condition will never be true.

I believe you want to step i with varStep, so change your loop to:

for (int i = varStart; i<varStop; i += varStep)
    // stuff inside loop

You use varStep++ in your for loop. I think you meant to do i+varStep. The application freezes because you're never increasing i, resulting in an endless loop.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top