Results 1 to 6 of 6
  1. #1
    MBP9004 is offline Member
    Join Date
    Mar 2013
    Posts
    6
    Rep Power
    0

    Default 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

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,007
    Blog Entries
    7
    Rep Power
    20

    Default Re: error: 'else' without 'if'

    Quote Originally Posted by MBP9004 View Post
    Java Code:
    							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");
    							}
    This part doesn't look healthy ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    MBP9004 is offline Member
    Join Date
    Mar 2013
    Posts
    6
    Rep Power
    0

    Default Re: error: 'else' without 'if'

    That's the bit that's confusing me, i'm not really sure what i'm doing wrong.

  4. #4
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default 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

  5. #5
    MBP9004 is offline Member
    Join Date
    Mar 2013
    Posts
    6
    Rep Power
    0

    Default 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;

  6. #6
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    7

    Default Re: error: 'else' without 'if'

    Quote Originally Posted by MBP9004 View Post

    Java Code:
    		for (int i=0; i<3; i++)
    			for (int j=0; j<3; j++)
    				if (b0 == b[i][j])
    				{	
    					x = i;   
    					y = j;   
    					return;
    				}
    I believe that this is a bad style for writing a code. It is better that we use code block for things like loops, if statements, etc, so that we clearly see the scope of our code. Writing code like this can easily introduce unwanted bugs in the program.

Similar Threads

  1. Replies: 3
    Last Post: 05-08-2012, 05:26 PM
  2. Replies: 1
    Last Post: 03-10-2010, 01:53 PM
  3. Replies: 1
    Last Post: 12-05-2009, 07:28 AM
  4. Replies: 1
    Last Post: 10-26-2009, 04:38 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •