Results 1 to 4 of 4
  1. #1
    Interista is offline Member
    Join Date
    Oct 2011
    Posts
    24
    Rep Power
    0

    Default Problem With An Else Statement - Probably Doing Something Simple Wrongly

    At line 22, the compiler is saying that there is an else statement without an if? How come? The if is there.

    Its a program designed to calculate the area of a circle, rectangle or triangle depending on the user's needs.

    I've only gone as far a circle as I feel if I get circle done then the others shouldn't be a huge problem as they should (HOPEFULLY) be a case of repetition. Sorry for all the comments in the code, that's howe have to present our work.

    Java Code:
    // Program to read in a shape from a user, using JOptionPane, then ask for measurement again using
    //JOptionPane then calculate the area
    
    import javax.swing.JOptionPane; //import JOptionPane
    public class CircleRectangleTriangle //create class name
    { //open class
    	public static void main (String[] args) //create args
    		{ //open args
    			int higher, lower; //declare ints
    			String circle=circle; //String with value circle
    			String rectangle=rectangle; //String with value rectangle
    			String triangle=triangle; //String with value triangle
    			String shape=JOptionPane.showInputDialog(null, "Do you want to calculate the area of a circle, rectangle or triangle?:"); //receive shape type
    				if (shape.equals(circle)) //if shape string equals circle string
    				{ //open if
    				String userinput=JOptionPane.showInputDialog(null, "Enter the value of the radius:"); //receive radius value
    				radius=Double.parseDouble(userinput); //Parses userinput string to a double
    					if (radius < 0); //If radius value is lower than 0
    					{ //open if
    					JOptionPane.showMessageDialog(null, "Radius value less than 0: cannot compute circle area"); //Inform user of error
    					} //close if
    					else //else
    					{ //open else
    					area = radius*radius*3.1416; //calculate size of area
    					JOptionPane.showMessageDialog(null, "A circle of radius " + radius + " has an area of " + area + "."); //Inform user of size of area
    					} //close else
    				} //close if
    			System.exit(0); //program terminates safely
    		} //close args
    } //close class

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,037
    Rep Power
    10

    Default Re: Problem With An Else Statement - Probably Doing Something Simple Wrongly

    You if statement ends in a semicolon.

    if(whatever); //nothing happens
    {
    ///always executes
    }
    else{
    ///else without if
    }
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Interista is offline Member
    Join Date
    Oct 2011
    Posts
    24
    Rep Power
    0

    Default Re: Problem With An Else Statement - Probably Doing Something Simple Wrongly

    Great, thanks! Still get confused over syntax.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,037
    Rep Power
    10

    Default Re: Problem With An Else Statement - Probably Doing Something Simple Wrongly

    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Simple program, simple problem
    By taymilll in forum New To Java
    Replies: 12
    Last Post: 06-20-2011, 06:12 AM
  2. Help with simple control statement
    By Kevin BSU in forum New To Java
    Replies: 3
    Last Post: 11-15-2010, 05:27 PM
  3. a simple if statement
    By senca in forum New To Java
    Replies: 16
    Last Post: 02-25-2010, 10:17 PM
  4. Simple "if" statement problem....compiling error.
    By CYANiDE in forum New To Java
    Replies: 4
    Last Post: 10-14-2009, 10:56 PM
  5. Replies: 5
    Last Post: 08-15-2009, 06:57 AM

Posting Permissions

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