Welcome to Dream.In.Code
Become a Java Expert!

Join 150,105 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,890 people online right now. Registration is fast and FREE... Join Now!




Compiling Errors

 
Reply to this topicStart new topic

Compiling Errors, Checkerboard

JWD
26 Jun, 2008 - 10:32 PM
Post #1

New D.I.C Head
*

Joined: 13 Jun, 2008
Posts: 35


My Contributions
Hi there, can someone please help me with my compiling errors in my checkerboard assignment.
Thank you

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();
        Label stopLabel = new Label("Stop");
        TextField stopField = new TextField();
        Label stepLabel = new Label("Step");
        TextField stepField = new TextField();

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

            for (int i=0; i<16; i++)
            {
                boardDisplay[i] = new TextArea(null,3,5,3);
                if(i<16)
                    boardDisplay[i].getText();
                else
                    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 void actionPerformed(ActionEvent e)
            {
                if (i < 15)
                        start = Integer.parseInt(startField.getText());
                        stop = Integer.parseInt(stopField.getText());
                        step = Integer.parseInt(stepField.getText());

                    for (int i=0; i<16; i++)

                        boardDisplay[i].setBackground(Color.magenta);

                    for(int i=start; i<stop; i+=step)

                        boardDisplay[i].setBackground(Color.yellow);
                else

                    JOptionPane.showMessageDialog(null,"You must enter a value less than 15."."Error",JOptionPane.ERROR_MESSAGE);
            }
            public static void main(String[]args)
            {
                Checkerboard f = new Checkerboard();
                f.setBounds(50,100,300,400);
                f.setTitle("Checkerboard Array");
                f.setVisible(true);
            }
        }


Compiling Errors:

C:\Users\Documents\Java Applications\Chapter05\myCheckerboard.java:93: 'else' without 'if'
else
^
C:\Users\Documents\Java Applications\Chapter05\myCheckerboard.java:81: cannot resolve symbol
symbol : variable i
location: class myCheckerboard
if (i < 15)
^
.\Checkerboard.java:71: illegal start of expression
public void actionPerformed(ActionEvent e)
^
.\Checkerboard.java:93: ';' expected
}
^
.\Checkerboard.java:12: Checkerboard should be declared abstract; it does not define actionPerformed(java.awt.event.ActionEvent) in Checkerboard
public class Checkerboard extends Frame implements ActionListener
^
5 errors




Tool completed with exit code 1

User is offlineProfile CardPM
+Quote Post

AmitTheInfinity
RE: Compiling Errors
26 Jun, 2008 - 10:57 PM
Post #2

C Surfing ∞
Group Icon

Joined: 25 Jan, 2007
Posts: 1,162



Thanked: 44 times
Dream Kudos: 125
My Contributions
QUOTE(JWD @ 27 Jun, 2008 - 12:02 PM) *

Hi there, can someone please help me with my compiling errors in my checkerboard assignment.
Thank you

java


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();
Label stopLabel = new Label("Stop");
TextField stopField = new TextField();
Label stepLabel = new Label("Step");
TextField stepField = new TextField();

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

for (int i=0; i<16; i++)
{
boardDisplay[i] = new TextArea(null,3,5,3);
if(i<16)
boardDisplay[i].getText();
else
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 void actionPerformed(ActionEvent e)
{
if (i < 15)
start = Integer.parseInt(startField.getText());
stop = Integer.parseInt(stopField.getText());
step = Integer.parseInt(stepField.getText());

for (int i=0; i<16; i++)

boardDisplay[i].setBackground(Color.magenta);

for(int i=start; i<stop; i+=step)

boardDisplay[i].setBackground(Color.yellow);
else

JOptionPane.showMessageDialog(null,"You must enter a value less than 15."."Error",JOptionPane.ERROR_MESSAGE);
}
public static void main(String[]args)
{
Checkerboard f = new Checkerboard();
f.setBounds(50,100,300,400);
f.setTitle("Checkerboard Array");
f.setVisible(true);
}
}


Compiling Errors:

C:\Users\Documents\Java Applications\Chapter05\myCheckerboard.java:93: 'else' without 'if'
else
^
C:\Users\Documents\Java Applications\Chapter05\myCheckerboard.java:81: cannot resolve symbol
symbol : variable i
location: class myCheckerboard
if (i < 15)
^
.\Checkerboard.java:71: illegal start of expression
public void actionPerformed(ActionEvent e)
^
.\Checkerboard.java:93: ';' expected
}
^
.\Checkerboard.java:12: Checkerboard should be declared abstract; it does not define actionPerformed(java.awt.event.ActionEvent) in Checkerboard
public class Checkerboard extends Frame implements ActionListener
^
5 errors




Tool completed with exit code 1


your if at line 73 has a block of code and you didn't put it in braces, which is making compiler that if has only the very next statement in it's block and does not have any else. so when it comes down to line 85 it finds an else without if.

on line 73 you are using variable i in your if but this variable is not defined in that method! [and if you define it it will generate redefinition error for those 2 for loops so make sure to remove int from the definition of i inside those loops]

you have a class called myCheckerboard and you are making object of Checkerboard?

I am not expert in Java so I have just given my observations here, see if this can help you. smile.gif
User is online!Profile CardPM
+Quote Post

Ellie
RE: Compiling Errors
27 Jun, 2008 - 02:41 AM
Post #3

D.I.C Regular
Group Icon

Joined: 17 Jan, 2007
Posts: 428



Thanked: 4 times
Dream Kudos: 150
My Contributions
Try this

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();
        Label stopLabel = new Label("Stop");
        TextField stopField = new TextField();
        Label stepLabel = new Label("Step");
        TextField stepField = new TextField();

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

            for (int i=0; i<16; i++)
            {
                boardDisplay[i] = new TextArea(null,3,5,3);
                if(i<16)
                    boardDisplay[i].getText();
                else
                    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 void actionPerformed(ActionEvent e)
            {
               // if (i < 15) {
                        start = Integer.parseInt(startField.getText());
                        stop = Integer.parseInt(stopField.getText());
                        step = Integer.parseInt(stepField.getText());

                    for (int i=0; i<16; i++)

                        boardDisplay[i].setBackground(Color.magenta);

                    for(int i=start; i<stop; i+=step)

                        boardDisplay[i].setBackground(Color.yellow);

                    //}
                //else

                  //  JOptionPane.showMessageDialog(null,"You must enter a value less than 15.","Error",JOptionPane.ERROR_MESSAGE);
            }
            public static void main(String[]args)
            {
                myCheckerboard f = new myCheckerboard();
                f.setBounds(50,100,300,400);
                f.setTitle("Checkerboard Array");
                f.setVisible(true);
            }
        }


But I have commented out your line 75:

CODE
// if (i < 15) {


Because I don't know what you are trying to test. There is no i variable within the scope of that method, and I can't work out what you're testing for? Hopefully this working version above will give you something to work from, and you can post again with any other questions. icon_up.gif
User is offlineProfile CardPM
+Quote Post

JWD
RE: Compiling Errors
27 Jun, 2008 - 03:31 AM
Post #4

New D.I.C Head
*

Joined: 13 Jun, 2008
Posts: 35


My Contributions
Hi, thank you for your reply. The checkerboard is suppose to demonstrate arrays and looping structures. Each field should have a number from 1 - 15 with the first left top field blank... that I still have to figure out... and when the user enters a value between 1 & 15 the fields are suppose to change color.

But I will try to add all of that first and see where it takes me next. Thank you for your help... I seem to be getting more help from the people on this site than what I do from the Collage I'm studying at.

Thanx again
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 12:58AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month