Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 105,765 Java Programmers for FREE! Ask your question and get quick answers from experts. There are 1,597 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Compiling Errors

 
Reply to this topicStart new topic

Compiling Errors, Checkerboard

JWD
post 1 Jul, 2008 - 12:28 AM
Post #1


New D.I.C Head

*
Joined: 13 Jun, 2008
Posts: 24


My Contributions


Hi there, can someone please help:
This application is suppose to change color to correspond with the start, stop and step values entered by the user and to create a looping structurethat allows the user to specify how many times the loop statements will be executed.

CODE


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

public class myCheckerboard extends Frame implements ActionListener
{
    //declare variables
    int start, stop, step;

    Panel boardPanel = new Panel();
        TextArea boardDisplay[] = new TextArea[16];

    Panel buttonPanel = new Panel();
        Button goButton = new Button("Go");

    Panel inputPanel = new Panel();
        Label startLabel = new Label("Start");
        TextField startField = new TextField(2);
        Label stopLabel = new Label("Stop");
        TextField stopField = new TextField(2);
        Label stepLabel = new Label("Step");
        TextField stepField = new TextField(2);

    public myCheckerboard()
    {
        this.setLayout(new BorderLayout());
            boardPanel.setLayout(new GridLayout(4,4,2,3));
            inputPanel.setLayout(new FlowLayout());
            buttonPanel.setLayout(new FlowLayout(FlowLayout.LEFT));

            for (int i=0; i<16; i++)
            {
                boardDisplay[i] = new TextArea(null,3,5,3);
                if(i<16)
                    boardDisplay[i].setText("" + i);
                    boardDisplay[i].setEditable(false);
                    boardDisplay[i].setBackground(Color.white);
                    boardPanel.add(boardDisplay[i]);
            }

            //add components to the button
            buttonPanel.add(goButton);

            //add components to input panel
            inputPanel.add(startLabel);
            inputPanel.add(startField);
            inputPanel.add(stopLabel);
            inputPanel.add(stopField);
            inputPanel.add(stepLabel);
            inputPanel.add(stepField);

            //add panels to frame
            add(buttonPanel, BorderLayout.SOUTH);
            add(inputPanel, BorderLayout.CENTER);
            add(boardPanel, BorderLayout.NORTH);

            goButton.addActionListener(this);

            addWindowListener(
                new WindowAdapter()
                {
                    public void windowClosing(WindowEvent e)
                    {
                        System.exit(0);
                    }
                }
                );
            }//end of constructor

            public static void main(String[]args)
            {
                myCheckerboard f = new myCheckerboard();
                f.setBounds(50,100,300,400);
                f.setTitle("Checkerboard Array");
                f.setVisible(true);
            }

            public void actionPerformed(ActionEvent e)
            {
                if(i>15)
                {
                    JOptionPane.showMessageDialog(null, "You must enter a value less than 15.","Error",JOptionPane.ERROR_MESSAGE);
                }
                else
                {
                    start = Integer.parseInt(startField.getText());
                    stop = Integer.parseInt(stopField.getText());
                       step = Integer.parseInt(stepField.getText());
                }
                for (int i = 0; i <15; i++)
                {
                    panelDisplay[i].setBackground(Color.magenta);
                }
                for(int i = start; i < stop; i += step)
                {
                    panelDisplay[i].setBackground(Color.yellow);
                }
            }
        }



Compiling Errors:

C:\Users\Patricia\Documents\Java Applications\myCheckerboard.java:88: cannot resolve symbol
symbol : variable i
location: class myCheckerboard
if(i>15)
^
C:\Users\Patricia\Documents\Java Applications\myCheckerboard.java:100: cannot resolve symbol
symbol : variable panelDisplay
location: class myCheckerboard
panelDisplay[i].setBackground(Color.magenta);
^
C:\Users\Patricia\Documents\Java Applications\myCheckerboard.java:104: cannot resolve symbol
symbol : variable panelDisplay
location: class myCheckerboard
panelDisplay[i].setBackground(Color.yellow);
^
3 errors

Tool completed with exit code 1



Hope someone can help
User is offlineProfile CardPM

Go to the top of the page


Martyr2
post 1 Jul, 2008 - 12:41 AM
Post #2


Programming Theoretician

Group Icon
Joined: 18 Apr, 2007
Posts: 4,188



Thanked 62 times

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions


First of all you defined the variable "i" inside a for loop of the mycheckerboard method. That means that the scope of that variable is the lifetime of the loop. No other code outside the loop can see it. That means you will have to either make "i" a higher scope or redefine it in "actionPerformed". I suggest the higher scope solution.

Your second and third errors is because no where do you define panelDisplay. Remember you must always define a variable/array before you can use it. Could you have called it by a different name up top in the variable declarations?

Once you fix those then the errors will be cleared up. smile.gif
User is offlineProfile CardPM

Go to the top of the page

AmitTheInfinity
post 1 Jul, 2008 - 01:30 AM
Post #3


D.I.C Addict

Group Icon
Joined: 25 Jan, 2007
Posts: 535



Thanked 9 times

Dream Kudos: 125
My Contributions


And please don't double post your topics. mad.gif
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 8/21/08 02:28PM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month