Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

2 errors In Checkerboard java code Please Help?

Hi Everyone

could someone please help me correct these 2 errors. This program is to create an application that demonstrates Array and Looping structures.

Code: import java.awt.*;

import java.awt.event.*;

import javax.swing.JOptionPane;

public class Checkerboard extends Frame implements ActionListener

{

//Declare the Panels that holds the Array

Panel boardPanel = new Panel();

TextArea numberArea[] = new TextArea[16];

////Declares the Panel that holds three fiels and labels

Panel buttonPanel = new Panel();

Button goBut = new Button("Go");

Label startLabel = new Label("Start");

Label stepLabel = new Label("Step");

Label stopLabel = new Label("Stop");

Panel inputPanel = new Panel();

int start=0, stop=0, step=0;

TextField startField = new TextField();

TextField stepField = new TextField();

TextField stopField = new TextField();

public Checkerboard()

{

// set layouts for frame and two panels

this.setLayout(new BorderLayout());

boardPanel.setLayout(new GridLayout(4,4));

inputPanel.setLayout(new GridLayout(2,3));

buttonPanel.setLayout(new FlowLayout(FlowLayout.LEFT));

// add components to board Panel

for (int i = 0; i < numberArea.length; i++) {

numberArea[i] = new TextArea(null, 3, 5, 3);

numberArea[i].setText(Integer.toString(i));

numberArea[i].setEditable(false);

numberArea[i].setBackground(Color.WHITE);

boardPanel.add(numberArea[i]);

}

// add components to input Panel

inputPanel.add(startField);

inputPanel.add(stepField);

inputPanel.add(stopField);

inputPanel.add(startLabel);

inputPanel.add(stepLabel);

inputPanel.add(stopLabel);

//buttonPanel Component

buttonPanel.add(goBut);

// add panels to frame

add(boardPanel, BorderLayout.NORTH);

add(inputPanel, BorderLayout.CENTER);

add(buttonPanel, BorderLayout.SOUTH);

goBut.addActionListener(this);

// overriding the windowClosing() method will allow the user to click the Close button

addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

System.exit(0);

}

});

}// end of constructor method

public static void main(String[] args)

{

Checkerboard f = new Checkerboard();

f.setBounds(50, 100, 300, 400);

f.setTitle("Checkerboard Array");

f.setVisible(true);

}// end of main() method

public void actionPerformed(ActionEvent e)

{

String arg = e.getActionCommand();

try

{

start = Integer.parseInt(startField.getText());

step = Integer.parseInt(stepField.getText());

stop = Integer.parseInt(stopField.getText());

}catch(Exception exception)

{

return;

}

Object src = e.getSource();

for (int x = start -1; x < stop; x = step) //you need to do the -1 because the array subscript values in Java all start

//with zero, but humans use numbers starting with 1

{

//numberArea[x] = numberArea[start & start++].setBackground(Color.magenta);//..change the color for that square here

if (src == goBut)

{

if (start >= 0 && start <= 15 && start < stop) {

numberArea[x] = numberArea[start + start++].setBackground(Color.magenta);

}

else

{

JOptionPane

.showMessageDialog(

null,

"You have entered an incorrect number or a number larger than 15. Please try again",

"Error", JOptionPane.ERROR_MESSAGE);

}

if (step >= 0 && step <= 15 && start < stop) {

numberArea[x] = numberArea[step + step++].setBackground(Color.yellow);

}

}

}

}

}

Errors: C:\Users\user\Desktop\Checkerboard.java:111: incompatible types

found : void

required: java.awt.TextArea

numberArea[x] = numberArea[start + start++].setBackground(Color.magenta);

^

C:\Users\user\Desktop\Checkerboard.java:124: incompatible types

found : void

required: java.awt.TextArea

numberArea[x] = numberArea[step + step++].setBackground(Color.yellow);

3 Answers

Relevance
  • Anonymous
    9 years ago
    Favorite Answer

    The problem is here:

    numberArea[x] = numberArea[start + start++].setBackground(Color.magenta);

    Try to understand what this line is doing.

    it is trying to make the left side ( numberArea[x] ) equal to the expression on the right.

    The right hand side is actually a method, not an object.. the method is setBackground.

    When a method is called, it may return a value. The returned value is what would be stored into numberArea[x]

    the setBackground method does not return a value (this is the same as returning 'void')

    I'm not totally sure what you want this line to do,

    If you want the left (an entry in an array) to equal the contents of a higher entry in the same array, it should look like

    numberArea[x] = numberArea[step + step++];

    (make sure you understand what step++ is actually doing here)

    you will need to call the setBackground method on a different line.

    you don't need to use an equals to do this.

    numberArea[x].setBackground(Color.yellow);

    As i said, its not clear what you want to happen on this line, but hopefully now you understand where the problem is and can sort it yourself.

    Source(s): BSc computer science
  • 9 years ago

    Take a quick look at the documentation for JTextArea. I think it may not have a value and this is causing the incompatible types...void. Here is where I think the problem is:

    // add components to board Panel

    for (int i = 0; i < numberArea.length; i++) {

    ****numberArea[i] = new TextArea(null, 3, 5, 3); //CHECK OUT THIS LINE

    numberArea[i].setText(Integer.toString(i…

    numberArea[i].setEditable(false);

    numberArea[i].setBackground(Color.WHITE)…

    boardPanel.add(numberArea[i]);

    }

    I think it should be something like, TextArea("String",5,10) but I am not 100% sure. Most likely this issue relates to both error codes.

    Source(s): Google, BS in Computer Science
  • Anonymous
    9 years ago

    Use these lines :

    numberArea[x] = numberArea[start + start++];

    numberArea[x].setBackground(Color.magenta);

    Instead of this:

    numberArea[x] = numberArea[start + start++].setBackground(Color.magenta);

    Use these lines:

    numberArea[x] = numberArea[step + step++];

    numberArea[x].setBackground(Color.yellow);

    Instead of this:

    numberArea[x] = numberArea[step + step++].setBackground(Color.yellow);

Still have questions? Get your answers by asking now.