Results 1 to 6 of 6
Thread: error: 'else' without 'if'
- 03-05-2013, 11:28 AM #1
Member
- Join Date
- Mar 2013
- Posts
- 6
- Rep Power
- 0
error: 'else' without 'if'
I've been trying to work this one out most common problem seems to be semi colon or overuse of curly bracket but i'm not too sure whats wrong with mine.
Java Code:import java.awt.*; import javax.swing.*; import java.awt.event.*; class MyFrame extends JFrame { JButton[][] b = new JButton[3][3]; int x, y; // clicked button private void findClickedButton(JButton[][] b, JButton b0) { // sets outer x, y to the indices of where 'b0' is in 'b' for (int i=0; i<3; i++) for (int j=0; j<3; j++) if (b0 == b[i][j]) { x = i; y = j; return; } } public MyFrame() { setTitle("Test Event Handling 3"); setSize(300, 300); setLocation(100, 100); Container c = getContentPane(); c.setLayout(new GridLayout(3, 3)); int count = 0; for (int i=0; i<3; i++) for (int j=0; j<3; j++) { b[i][j] = new JButton(); c.add(b[i][j]); b[i][j].addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { if(count %2) { JButton b0 = (JButton) e.getSource(); findClickedButton (b, b0); // coords placed in x, y b[x][y].setText("X"); else b[x][y].setText("O"); } } }); } setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); setVisible(true); } // MyFrame constructor } // MyFrame
- 03-05-2013, 11:35 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,603
- Blog Entries
- 7
- Rep Power
- 17
- 03-05-2013, 12:29 PM #3
Member
- Join Date
- Mar 2013
- Posts
- 6
- Rep Power
- 0
Re: error: 'else' without 'if'
That's the bit that's confusing me, i'm not really sure what i'm doing wrong.
- 03-05-2013, 01:05 PM #4
Re: error: 'else' without 'if'
The else statement is within the if statement.
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 03-05-2013, 03:36 PM #5
Member
- Join Date
- Mar 2013
- Posts
- 6
- Rep Power
- 0
Re: error: 'else' without 'if'
Got it working :)
Java Code:if(count %2 == 0) { JButton b0 = (JButton) e.getSource(); findClickedButton (b, b0); // coords placed in x, y b[x][y].setText("X"); } else { JButton b1 = (JButton) e.getSource(); findClickedButton (b, b1); // coords placed in x, y b[x][y].setText("O"); } count = count + 1;
- 03-06-2013, 04:44 AM #6
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 697
- Rep Power
- 6
Similar Threads
-
java.lang.NullpointerException error in tomcat 5.5 / http status 500 error
By rahil in forum Apache CommonsReplies: 3Last Post: 05-08-2012, 05:26 PM -
> Operator cannot be applied error and return incompatible types error
By corney_16 in forum New To JavaReplies: 1Last Post: 03-10-2010, 01:53 PM -
Thread: Error 500--Internal Server Error java.lang.NullPointerException
By jackdear44 in forum New To JavaReplies: 1Last Post: 12-05-2009, 07:28 AM -
java.lang.Error: Error opening DSound for capture
By NARs in forum NetworkingReplies: 1Last Post: 10-26-2009, 04:38 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks