Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Minefield Program :: 'else' before 'if error?

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Cool Minefield Program :: 'else' before 'if error?

    Hi guys,

    First post here, but I've been viewing the forums for a long time. I had to write this program for my Java II Class in school (I'm in highschool). I can't seem to figure out what the problem is here that is keeping me.

    import javax.swing.*;
      import java.awt.*;
      import java.awt.event.*;
     
      public class Minefield extends JFrame implements ActionListener
      {
     
      	JPanel[] aPanel = new JPanel[20];
      	JButton[] aButton = new JButton[20];
      	int bomb = (int) (Math.random() * 100) % 20;
      	int counter;
      	JLabel bLab = new JLabel("");
     
      	public Minefield()
      	{
      		setTitle("Mine Field");
      		setSize(500, 500);
      		setVisible(true);
      		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      		setLayout(new GridLayout(5, 5));
     
      		for (int i = 0; i<aPanel.length; i++)
      		{
      			aPanel[i] = new JPanel(new GridLayout(1, 1));
      			aButton[i] = new JButton("");
      			aButton[i].setBackground(Color.BLUE);
      			add(aPanel[i]);
      			aPanel[i].add(aButton[i]);
      			aButton[i].addActionListener(this);
     
      		}//end forloop
     
      	}
     
      	public void actionPerformed(ActionEvent e)
      	{
      		Object source = e.getSource();
      		counter++;
      		for (int i = 0; i<aButton.length; i++)
      		{
      			if(aButton[i] == source)
      			{
      				if(i == bomb);
      				{
      					aButton[i].setBackground(Color.RED);
     
      					for (int a = 0; i<aButton.length; a++)
      					{
      						if(a != bomb)
      						{
      							aButton[a].setBackground(Color.WHITE);
      						}
      					}
      				}
      				else
      				{
      					aButton[i].setBackground(Color.WHITE);
     
      					if(counter == 10)
      					{
      						bLab.setText("You win!");
      					}
      				}
      			}
      		}//end forloop
      	}
     
      	public static void main(String[] args)
      	{
      		Minefield b1 = new Minefield();
      	}//END MAIN METHOD
      }//END CLASS Minefield


    Any help would be very appreciated! Thanks in advance


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Minefield Program :: 'else' before 'if error?

    What is the problem...does it compile? Are there exceptions? Does it behave incorrectly? This information is important for us to help you solve your issue(s)

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Minefield Program :: 'else' before 'if error?

    if(i == bomb);
    You have a semi-colon after this if statement, which counts as the statement for the if. The block after the if then is always executed because it's not part of the if, and Java thinks you're putting an if by itself.

  4. The Following User Says Thank You to helloworld922 For This Useful Post:

    soul.salem (January 12th, 2011)

Similar Threads

  1. What's the damn error in this program...
    By bsudhir6 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 12th, 2010, 07:57 AM
  2. Error in Program for Game of Craps
    By TheAsianMenace in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 23rd, 2010, 04:31 AM
  3. Need help with program please: getting symbol error
    By blinkzz in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: November 18th, 2009, 02:23 AM
  4. Error of data types and type casting in java program
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: September 2nd, 2009, 10:22 AM
  5. Error while creating Gym member database through Java programming
    By parvez07 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: August 26th, 2009, 02:17 AM