n00b logic error?

Compiles, runs, does not work

Page 1 of 1

5 Replies - 613 Views - Last Post: 16 September 2009 - 08:42 PM Rate Topic: -----

#1 livewire120v  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 16-September 09

n00b logic error?

Post icon  Posted 16 September 2009 - 05:24 AM

/*
Assigment:
Programmer:	
Due Date:	August 28, 2009
Program:	Checkerboard.java
Purpose:
*/

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

public class checkerboard extends Frame implements ActionListener
{ // start class
	private TextField textArray[]=new TextField[16];
	private Panel arrayPanel=new Panel();
	private Panel fieldPanel=new Panel();
	private Panel buttonPanel=new Panel();
	private TextField startTF;
	private TextField stopTF;
	private TextField stepTF;
	private int start;
	private int stop;
	private int step;
	private Label startLabel=new Label("Start");
	private Label stopLabel=new Label("Stop");
	private Label stepLabel=new Label("Step");
	private Button clearButton=new Button("Clr");
	private Button goButton=new Button("Go");

public checkerboard()
{ // start checkerboard frame
startTF = new TextField(10);
stopTF = new TextField(10);
stepTF = new TextField(10);
start = 0;
stop = 15;
step = 0;

for(int i=0; i<16; ++i)
	{ // start for loop
		textArray[i] = new TextField();
		textArray[i].setEditable(false);
		textArray[i].setText(String.valueOf(i));
		textArray[i].setBackground(Color.white);
	} // end for loop

setLayout(new BorderLayout(2,2)); // set frame layout manager
// set Panel layout manager
arrayPanel.setLayout(new GridLayout(4,4,30,30));
fieldPanel.setLayout(new GridLayout(2,3,10,10));
buttonPanel.setLayout(new GridLayout(1,2,20,20));
// Add TestFields to Array Panels
for(int j=0; j<16; ++j)
arrayPanel.add(textArray[j]);
// Add textfieldtotextpanel
fieldPanel.add(startTF);
fieldPanel.add(stopTF);
fieldPanel.add(stepTF);
fieldPanel.add(startLabel);
fieldPanel.add(stopLabel);
fieldPanel.add(stepLabel);
buttonPanel.add(goButton);
buttonPanel.add(clearButton);
add(buttonPanel,BorderLayout.SOUTH);
add(fieldPanel,BorderLayout.CENTER);
add(arrayPanel,BorderLayout.NORTH);
startTF.addActionListener(this);
stopTF.addActionListener(this);
stepTF.addActionListener(this);
goButton.addActionListener(this);
clearButton.addActionListener(this);
setVisible(true);
startTF.requestFocus();
addWindowListener(
	new WindowAdapter()
	{ // start window adapter
		public void windowClosing(WindowEvent e)
		{ // start window event
			System.exit(0);
		} // end window event
	} // end window adapter
	);
} // end checkerboard frame

public void actionPerformed(ActionEvent e)
{ // start action performed
	String arg = e.getActionCommand();
	try
	{ // start try
		if(arg == "Go")
		{ // start if
			start = Integer.parseInt(startTF.getText());
			stop = Integer.parseInt(stopTF.getText());
			step = Integer.parseInt(stepTF.getText());
			for(int i=0; i<16; ++i)
			textArray[i].setBackground(Color.blue);
			for(int i=start; i>=stop; i+=step)
			textArray[i].setBackground(Color.yellow);
		} // end if
 	} // end try
catch(Exception x)
{ // start catch
	JOptionPane.showMessageDialog(null,"Data Entry Error",
	"Error", JOptionPane.INFORMATION_MESSAGE);
	arg = "clear";
} // end catch

if(arg == "clear");
startTF.setText("");
stopTF.setText("");
stepTF.setText("");
for(int i=0; i<16; ++i)
	textArray[i].setBackground(Color.white);
} // end action performed

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



Greetings all,
I have been stuck on this for weeks and it is way past due. I managed to get it to compile and run but it does not work. Here is what it is supposed to do:
There is a frame with 16 textfields, a start,stop, step textfields and a go, clear button. You type in a start, a stop, and a step integer. When you click the go button your start and step should turn yellow and the rest blue. You can clear the fields and use new data.
I can compile, run and enter the numbers but when I hit go every thing clears!
I think I have placed my clear arg in the wrong place or maybe miss used the braces.
This is a homework assignment. I just do not understand why it skips to clear (did I leave out a return?)
Thank you,
livewire120v
PS I am new here.

Is This A Good Question/Topic? 0
  • +

Replies To: n00b logic error?

#2 AntonWebsters  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 88
  • View blog
  • Posts: 428
  • Joined: 15-August 09

Re: n00b logic error?

Posted 16 September 2009 - 05:47 AM

for(int i=start; i>=stop; i+=step)
				textArray[i].setBackground(Color.yellow);


Umm..shouldn't it be i<=stop;?

By the way,I think your error is at this part of your code..
if(arg == "clear"){
startTF.setText("");
stopTF.setText("");
stepTF.setText("");
for(int i=0; i<16; ++i)
	textArray[i].setBackground(Color.white);
} // end action performed
}


You missed out the curly-brackets...
Without the brackets, the background color of the text fields will always change to white at the end.

This post has been edited by AntonWebsters: 16 September 2009 - 05:54 AM

Was This Post Helpful? 0
  • +
  • -

#3 AntonWebsters  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 88
  • View blog
  • Posts: 428
  • Joined: 15-August 09

Re: n00b logic error?

Posted 16 September 2009 - 05:59 AM


/*
Assigment:
Programmer:	
Due Date:	August 28, 2009
Program:	Checkerboard.java
Purpose:
*/

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

public class checkerboard extends Frame implements ActionListener
{ // start class
	private TextField textArray[]=new TextField[16];
	private Panel arrayPanel=new Panel();
	private Panel fieldPanel=new Panel();
	private Panel buttonPanel=new Panel();
	private TextField startTF;
	private TextField stopTF;
	private TextField stepTF;
	private int start;
	private int stop;
	private int step;
	private Label startLabel=new Label("Start");
	private Label stopLabel=new Label("Stop");
	private Label stepLabel=new Label("Step");
	private Button clearButton=new Button("Clr");
	private Button goButton=new Button("Go");

public checkerboard()
{ // start checkerboard frame
startTF = new TextField(10);
stopTF = new TextField(10);
stepTF = new TextField(10);
start = 0;
stop = 15;
step = 0;

for(int i=0; i<16; ++i)
	{ // start for loop
		textArray[i] = new TextField();
		textArray[i].setEditable(false);
		textArray[i].setText(String.valueOf(i));
	} // end for loop

setLayout(new BorderLayout(2,2)); // set frame layout manager
// set Panel layout manager
arrayPanel.setLayout(new GridLayout(4,4,30,30));
fieldPanel.setLayout(new GridLayout(2,3,10,10));
buttonPanel.setLayout(new GridLayout(1,2,20,20));
// Add TestFields to Array Panels
for(int j=0; j<16; ++j)
arrayPanel.add(textArray[j]);
// Add textfieldtotextpanel
fieldPanel.add(startTF);
fieldPanel.add(stopTF);
fieldPanel.add(stepTF);
fieldPanel.add(startLabel);
fieldPanel.add(stopLabel);
fieldPanel.add(stepLabel);
buttonPanel.add(goButton);
buttonPanel.add(clearButton);
add(buttonPanel,BorderLayout.SOUTH);
add(fieldPanel,BorderLayout.CENTER);
add(arrayPanel,BorderLayout.NORTH);
startTF.addActionListener(this);
stopTF.addActionListener(this);
stepTF.addActionListener(this);
goButton.addActionListener(this);
clearButton.addActionListener(this);
setVisible(true);
startTF.requestFocus();
addWindowListener(
	new WindowAdapter()
	{ // start window adapter
		public void windowClosing(WindowEvent e)
		{ // start window event
			System.exit(0);
		} // end window event
	} // end window adapter
	);
} // end checkerboard frame

public void actionPerformed(ActionEvent e)
{ // start action performed
	String arg = e.getActionCommand();
	try
	{ // start try
		if(arg.equals("Go"))
		{ // start if
			start = Integer.parseInt(startTF.getText());
			stop = Integer.parseInt(stopTF.getText());
			step = Integer.parseInt(stepTF.getText());
			for(int i=0; i<16; ++i)
				textArray[i].setBackground(Color.blue);
			for(int i=start; i<=stop; i+=step) 
				textArray[i].setBackground(Color.yellow);
		}
		if(arg.equals("Clr")){
			startTF.setText("");
			stopTF.setText("");
			stepTF.setText("");
			for(int i=0; i<16; ++i)
				textArray[i].setBackground(Color.white);
			} // end action performed
		// end if
	  // end try
	}
		
catch(Exception x)
{ // start catch
	JOptionPane.showMessageDialog(null,"Data Entry Error",
	"Error", JOptionPane.INFORMATION_MESSAGE);
	arg = "clear";
} // end catch
}

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


This post has been edited by AntonWebsters: 17 September 2009 - 12:10 AM

Was This Post Helpful? 0
  • +
  • -

#4 livewire120v  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 16-September 09

Re: n00b logic error?

Posted 16 September 2009 - 06:39 AM

Hey Thanks,
I am playing with the brackets and changed the greater than typo. I will get back to you when I get it right and what I did to fix it. If it does not run in a few hours it will be tomorrow since I have a new assignment and class.
But I will not forget you. Only DEATH or an extended power outage could keep me away from this site. I used it all quarter and should have joined sooner. Also, I will probably have to retake java ( I caught that flu [not h1-n1 but a1-z99]) :0| All better now but got 2 weeks behind. You just can not do that in learning a comp. language.
Thanks again,
livewire120v
Was This Post Helpful? 0
  • +
  • -

#5 AntonWebsters  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 88
  • View blog
  • Posts: 428
  • Joined: 15-August 09

Re: n00b logic error?

Posted 16 September 2009 - 06:54 AM

Quote

Only DEATH or an extended power outage could keep me away from this site.


Cool quote. ;) Well, good luck.
Was This Post Helpful? 0
  • +
  • -

#6 livewire120v  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 16-September 09

Re: n00b logic error?

Posted 16 September 2009 - 08:42 PM

OOooooh, I made a C in java. I am going to retake the class the next time it is offered. Sooooo, disappointed.
I showed a bunch of the answer for this program. I am not sure that I should post the final product.
Thanks to all of you so much. I really wished that I had not waited till the end of the quarter to actually join. I used this site frequently before I joined.
I think that I will have a cup daily during my two week break.
Truly,
livewire120v
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1