| | |
doing a program blinded
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jan 2008
Posts: 3,819
Reputation:
Solved Threads: 501
Jim you need to pay close attention to your run-time errors. You have a problem here:
You have two loops. Don't assume the problem is in the second one. Look closely at the numbers in the first loop, and look closely at the instructions regarding array indexes give by your teacher (see bold):
Also, did you get something like this?
You need to read these error messages carefully. They'll tell you exactly what the problem is and where it is usually.
JAVA Syntax (Toggle Plain Text)
for(int i = 0; i <=16; i++) topDisplay[i].setBackground(Color.blue); for(int i = start; i <= stop; step++) topDisplay[i].setBackground(Color.yellow);
•
•
•
•
write another loop that’s based on the user inputs. Each time the loop is executed, change the background color to yellow (so… start your loop at the user’s specified starting condition. You’ll stop at the user’s specified stopping value. You’ll change the fields to yellow every time you increment your loop based on the step value. REMEMBER: Your displayed values are 1 off from your index numbers!!)
Java Syntax (Toggle Plain Text)
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 16 at Checkerboard.actionPerformed(Checkerboard.java:117)
You need to read these error messages carefully. They'll tell you exactly what the problem is and where it is usually.
•
•
Join Date: Jan 2008
Posts: 189
Reputation:
Solved Threads: 0
got the yellow boxes to come up but just one box.....so is there something wrong with my yellow set background because I am not seeing any more errors
Java Syntax (Toggle Plain Text)
//packages to import import javax.swing.JOptionPane; import java.awt.*; import java.awt.event.*; public class Checkerboard extends Frame implements ActionListener { private Panel topPanel; private TextArea topDisplay[]; private Panel bottomPanel; private TextField startField = new TextField(10); private TextField stopField = new TextField(10); private TextField stepField = new TextField(10); private Label startLabel = new Label ("Start"); private Label stopLabel = new Label ("Stop"); private Label stepLabel = new Label ("Step"); private Button goButton; private Button clearButton; private boolean clearText; private boolean first; private int start; private int stop; private int step; //constructor methods public Checkerboard() { //construct components and initialize beginning values topPanel = new Panel(); topDisplay = new TextArea[16]; goButton = new Button("Go"); clearButton = new Button("Clear"); first = true; bottomPanel = new Panel(); int start = 0; int stop = 0; int step = 0; bottomPanel.add(startField); bottomPanel.add(stopField); bottomPanel.add(stepField); bottomPanel.add(startLabel); bottomPanel.add(stopLabel); bottomPanel.add(stepLabel); bottomPanel.add(goButton); goButton.addActionListener(this); bottomPanel.add(clearButton); clearButton.addActionListener(this); clearText = true; //set layouts for the Frame and Panels setLayout(new BorderLayout()); topPanel.setLayout(new GridLayout(4, 4, 10, 10)); bottomPanel.setLayout(new GridLayout(3, 3, 5, 5)); //construct the Display for(int i = 0; i <= 15; i++) { topDisplay[i] = new TextArea(null, 3, 5, 3); topDisplay[i].setText(String.valueOf(i+1)); topDisplay[i].setEditable(false); topPanel.add(topDisplay[i]); } //add components to frame add(topPanel, BorderLayout.NORTH); add(bottomPanel, BorderLayout.CENTER); //allow the x to close the application addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } } //end window adapter ); } public static void main(String args[]) { Checkerboard f = new Checkerboard(); f.setTitle("Checkerboard Array"); f.setBounds(50, 100, 300, 400); f.setLocationRelativeTo(null); f.setVisible(true); } //end main public void actionPerformed(ActionEvent e) { boolean done = false; //test go String arg = e.getActionCommand(); //go button was clicked if(arg.equals("Go")) { //convert data in TextField to int int start = Integer.parseInt(startField.getText()); int stop = Integer.parseInt(stopField.getText()); int step = Integer.parseInt(stepField.getText()); while(!done) { try { if((start <= 1) && (start > 16)) throw new NumberFormatException(); else done = true; } //end try catch (NumberFormatException f) { JOptionPane.showMessageDialog(null, "You must enter start between 1 and 16", "Error", JOptionPane.ERROR_MESSAGE); startField.setText(" "); startField.requestFocus(); } //end catch try { if ((stop < 1) && (stop > 16)) throw new NumberFormatException(); else done = true; } //end try catch (NumberFormatException f) { JOptionPane.showMessageDialog(null, "You must enter stop between 1 and 16", "Error", JOptionPane.ERROR_MESSAGE); stopField.setText(" "); stopField.requestFocus(); } //end catch try { if ((step < 1) && (step > 16)) throw new NumberFormatException(); else done = true; } //end try catch (NumberFormatException f) { JOptionPane.showMessageDialog(null, "You must enter step between 1 and 16", "Error", JOptionPane.ERROR_MESSAGE); stepField.setText(" "); stepField.requestFocus(); } //end catch try { if (start > stop) throw new NumberFormatException(); else done = true; } //end try catch (NumberFormatException f) { JOptionPane.showMessageDialog(null, "Stop cannot be larger than start", "Error", JOptionPane.ERROR_MESSAGE); startField.setText(" "); stopField.setText(" "); stepField.setText(" "); startField.requestFocus(); } //end catch } //end while for(int i = 0; i <=15; i++) topDisplay[i].setBackground(Color.blue); for(int i = start; i <= stop; step++) topDisplay[i].setBackground(Color.yellow); } //end the if go //clear button was clicked if(arg.equals("Clear")) { clearText = true; startField.setText(""); stopField.setText(""); stepField.setText(""); first = true; setBackground(Color.white); startField.requestFocus(); } //end the if clear }//end action listener }//end class
•
•
Join Date: Jan 2008
Posts: 189
Reputation:
Solved Threads: 0
Almost finished just working on my try\catch and I will be done can anyone let me know if I am on the right track and if I am any advice id greatly appreciate it if its before 11?
java Syntax (Toggle Plain Text)
//packages to import import javax.swing.JOptionPane; import java.awt.*; import java.awt.event.*; public class Checkerboard extends Frame implements ActionListener { private Panel topPanel; private TextArea topDisplay[]; private Panel bottomPanel; private TextField startField = new TextField(10); private TextField stopField = new TextField(10); private TextField stepField = new TextField(10); private Label startLabel = new Label ("Start"); private Label stopLabel = new Label ("Stop"); private Label stepLabel = new Label ("Step"); private Button goButton; private Button clearButton; private boolean clearText; private boolean first; private int start; private int stop; private int step; //constructor methods public Checkerboard() { //construct components and initialize beginning values topPanel = new Panel(); topDisplay = new TextArea[16]; goButton = new Button("Go"); clearButton = new Button("Clear"); first = true; bottomPanel = new Panel(); int start = 0; int stop = 0; int step = 0; bottomPanel.add(startField); bottomPanel.add(stopField); bottomPanel.add(stepField); bottomPanel.add(startLabel); bottomPanel.add(stopLabel); bottomPanel.add(stepLabel); bottomPanel.add(goButton); goButton.addActionListener(this); bottomPanel.add(clearButton); clearButton.addActionListener(this); clearText = true; //set layouts for the Frame and Panels setLayout(new BorderLayout()); topPanel.setLayout(new GridLayout(4, 4, 10, 10)); bottomPanel.setLayout(new GridLayout(3, 3, 5, 5)); //construct the Display for(int i = 0; i <= 15; i++) { topDisplay[i] = new TextArea(null, 3, 5, 3); topDisplay[i].setText(String.valueOf(i+1)); topDisplay[i].setEditable(false); topPanel.add(topDisplay[i]); } //add components to frame add(topPanel, BorderLayout.NORTH); add(bottomPanel, BorderLayout.CENTER); //allow the x to close the application addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } } //end window adapter ); } public static void main(String args[]) { Checkerboard f = new Checkerboard(); f.setTitle("Checkerboard Array"); f.setBounds(50, 100, 300, 400); f.setLocationRelativeTo(null); f.setVisible(true); } //end main public void actionPerformed(ActionEvent e) { boolean done = false; //test go String arg = e.getActionCommand(); //go button was clicked if(arg.equals("Go")) { //convert data in TextField to int int start = Integer.parseInt(startField.getText()); int stop = Integer.parseInt(stopField.getText()); int step = Integer.parseInt(stepField.getText()); while(!done) { try { if((start <= 1) && (start > 16)) throw new NumberFormatException(); else done = true; } //end try catch (NumberFormatException f) { JOptionPane.showMessageDialog(null, "You must enter start between 1 and 16", "Error", JOptionPane.ERROR_MESSAGE); startField.setText(" "); startField.requestFocus(); } //end catch try { if ((stop < 1) && (stop > 16)) throw new NumberFormatException(); else done = true; } //end try catch (NumberFormatException f) { JOptionPane.showMessageDialog(null, "You must enter stop between 1 and 16", "Error", JOptionPane.ERROR_MESSAGE); stopField.setText(" "); stopField.requestFocus(); } //end catch try { if ((step < 1) && (step > 16)) throw new NumberFormatException(); else done = true; } //end try catch (NumberFormatException f) { JOptionPane.showMessageDialog(null, "You must enter step between 1 and 16", "Error", JOptionPane.ERROR_MESSAGE); stepField.setText(" "); stepField.requestFocus(); } //end catch try { if (start > stop) throw new NumberFormatException(); else done = true; } //end try catch (NumberFormatException f) { JOptionPane.showMessageDialog(null, "Stop cannot be larger than start", "Error", JOptionPane.ERROR_MESSAGE); startField.setText(" "); stopField.setText(" "); stepField.setText(" "); startField.requestFocus(); } //end catch } //end while for(int i = 0; i <=15; i++) topDisplay[i].setBackground(Color.blue); for(int i = start-1; i < stop; i+=step) topDisplay[i].setBackground(Color.yellow); } //end the if go //clear button was clicked if(arg.equals("Clear")) { clearText = true; startField.setText(""); stopField.setText(""); stepField.setText(""); first = true; setBackground(Color.white); startField.requestFocus(); } //end the if clear }//end action listener }//end class
•
•
Join Date: Jan 2008
Posts: 189
Reputation:
Solved Threads: 0
last question and I will be done this is the problem I am having and not sure how to do this.....
only change the colors if the numbers are valid
only change the colors if the numbers are valid
java Syntax (Toggle Plain Text)
//packages to import import javax.swing.JOptionPane; import java.awt.*; import java.awt.event.*; public class Checkerboard extends Frame implements ActionListener { private Panel topPanel; private TextArea topDisplay[]; private Panel bottomPanel; private TextField startField = new TextField(10); private TextField stopField = new TextField(10); private TextField stepField = new TextField(10); private Label startLabel = new Label ("Start"); private Label stopLabel = new Label ("Stop"); private Label stepLabel = new Label ("Step"); private Button goButton; private Button clearButton; private boolean clearText; private boolean first; private int start; private int stop; private int step; //constructor methods public Checkerboard() { //construct components and initialize beginning values topPanel = new Panel(); topDisplay = new TextArea[16]; goButton = new Button("Go"); clearButton = new Button("Clear"); first = true; bottomPanel = new Panel(); int start = 0; int stop = 0; int step = 0; bottomPanel.add(startField); bottomPanel.add(stopField); bottomPanel.add(stepField); bottomPanel.add(startLabel); bottomPanel.add(stopLabel); bottomPanel.add(stepLabel); bottomPanel.add(goButton); goButton.addActionListener(this); bottomPanel.add(clearButton); clearButton.addActionListener(this); clearText = true; //set layouts for the Frame and Panels setLayout(new BorderLayout()); topPanel.setLayout(new GridLayout(4, 4, 10, 10)); bottomPanel.setLayout(new GridLayout(3, 3, 5, 5)); //construct the Display for(int i = 0; i <= 15; i++) { topDisplay[i] = new TextArea(null, 3, 5, 3); topDisplay[i].setText(String.valueOf(i+1)); topDisplay[i].setEditable(false); topPanel.add(topDisplay[i]); } //add components to frame add(topPanel, BorderLayout.NORTH); add(bottomPanel, BorderLayout.CENTER); //allow the x to close the application addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } } //end window adapter ); } public static void main(String args[]) { Checkerboard f = new Checkerboard(); f.setTitle("Checkerboard Array"); f.setBounds(50, 100, 300, 400); f.setLocationRelativeTo(null); f.setVisible(true); } //end main public void actionPerformed(ActionEvent e) { boolean done = false; //test go String arg = e.getActionCommand(); //go button was clicked if(arg.equals("Go")) { //convert data in TextField to int int start = Integer.parseInt(startField.getText()); int stop = Integer.parseInt(stopField.getText()); int step = Integer.parseInt(stepField.getText()); while(!done) { try { if((start <= 1) || (start > 16)) throw new NumberFormatException(); else done = true; } //end try catch (NumberFormatException f) { JOptionPane.showMessageDialog(null, "You must enter start between 1 and 16", "Error", JOptionPane.ERROR_MESSAGE); startField.setText(" "); startField.requestFocus(); } //end catch try { if ((stop < 1) || (stop > 16)) throw new NumberFormatException(); else done = true; } //end try catch (NumberFormatException f) { JOptionPane.showMessageDialog(null, "You must enter stop between 1 and 16", "Error", JOptionPane.ERROR_MESSAGE); stopField.setText(" "); stopField.requestFocus(); } //end catch try { if ((step < 1) || (step > 16)) throw new NumberFormatException(); else done = true; } //end try catch (NumberFormatException f) { JOptionPane.showMessageDialog(null, "You must enter step between 1 and 16", "Error", JOptionPane.ERROR_MESSAGE); stepField.setText(" "); stepField.requestFocus(); } //end catch try { if (start > stop) throw new NumberFormatException(); else done = true; } //end try catch (NumberFormatException f) { JOptionPane.showMessageDialog(null, "Stop cannot be larger than start", "Error", JOptionPane.ERROR_MESSAGE); startField.setText(" "); stopField.setText(" "); stepField.setText(" "); startField.requestFocus(); } //end catch } //end while for(int i = 0; i <=15; i++) topDisplay[i].setBackground(Color.blue); for(int i = start-1; i < stop; i+=step) topDisplay[i].setBackground(Color.yellow); } //end the if go //clear button was clicked if(arg.equals("Clear")) { clearText = true; startField.setText(""); stopField.setText(""); stepField.setText(""); first = true; setBackground(Color.white); startField.requestFocus(); } //end the if clear }//end action listener }//end class
![]() |
Similar Threads
- My URLs are blinded by their cloaks! (Geeks' Lounge)
Other Threads in the Java Forum
- Previous Thread: First, best, worst fit java program - need help debugging
- Next Thread: Curved vertex for swing containers.
| Thread Tools | Search this Thread |
actuate android api applet application applications array arrays automation balls bank binary bluetooth business c++ chat class clear client code codesnippet collections component coordinates database defaultmethod development dice doctype dragging ebook eclipse educational error formatingtextintooltipjava fractal froglogic game givemetehcodez graphics gui hql html ide image infinite ingres input integer intersect invokingapacheantprogrammatically j2me java javaprojects jni jpanel julia linux list loop looping map method methods mobile mysql netbeans newbie openjavafx parameter php print problem program programming project recursion repositories scanner screen scrollbar server set size sms sort sorting sql sqlserver state storm string sun superclass swing swt text-file threads tree windows






