Results 1 to 15 of 15
Like Tree2Likes
  • 1 Post By KevinWorkman
  • 1 Post By Tolls

Thread: Question about else if.

  1. #1
    tkelley1620 is offline Member
    Join Date
    May 2010
    Location
    Pittsburgh, PA
    Posts
    10
    Rep Power
    0

    Default Question about else if.

    Ok So i did some research and have re tried the program that I posted before.. I think that I got most of it..
    Just having problems with the if statesments and the else if statement.

    the error that I am getting is syntax system else; while expected.

    here is the code.

    Java Code:
    import javax.swing.JOptionPane;
    
    
    public class assignment3b {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		String name ="";
    		String Circle = null;
    		String radius;
    		double pi = 0;
    		double carea;
    		String Rectangle = null;
    		String base;
    		String height;
    		double rarea = 0;
    		String Right_Triangle = null;
    		String base1;
    		String height1;
    		double rtarea = 0;
    		String Square = null;
    		String base2;
    		double sarea;
    		int x = 5;
    				
    		do{
    		name = JOptionPane.showInputDialog(
    				null, " Please enter type of Shape: Circle, Rectangle, Right_Triangle or Square");
    		if (name == Circle);
    		{
    	    radius = JOptionPane.showInputDialog(
    	    		null," Please enter the radius of the circle");
    	    double r1 = Double.parseDouble(radius);
    	    carea = (pi * r1 * r1);
    		 {else if (name == Rectangle);
    		{
    		 base = JOptionPane.showInputDialog(
    				 null, "Please Enter the base of the Rectangle");
    		 double b = Double.parseDouble(base);
    		 height = JOptionPane.showInputDialog(
    				 "Please enter the Height of the Rectangle");
    		 double h = Double.parseDouble(height);
    		 rarea = (b * h);
    		} else if (name ==Right_Triangle);
    		{
    		base1 = JOptionPane.showInputDialog(
    				null, "Please Enter the Base of the Right Triangle");
    		double b1 =Double.parseDouble(base1);
    		height1 = JOptionPane.showInputDialog(
    				"Please Enter the Height of the Right Triangle");
    		double h1 = Double.parseDouble(height1);
    		rtarea = (b1 * h1)/2;
    		} else  (name == Square);
    		{
    		 base2 = JOptionPane.showInputDialog(
    				 null, "Please Enter the Base of the Square");
    		 double b2 = Double.parseDouble(base2);
    		 sarea = (b2 * b2);
    		 while (x < 6);
    		}
    		
    		 }
    		}
    		}
    	}
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,932
    Rep Power
    8

    Default Re: Question about else if.

    Yikes, check your indentation, and make sure that you don't do this:

    Java Code:
    if(something); //that semicolon is the end of the if statement!
    {
       //always gets called
    }
    The same thing applies to while, else, for, etc.
    Tolls likes this.
    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
    filestream is offline Member
    Join Date
    Mar 2012
    Posts
    8
    Rep Power
    0

    Default Re: Question about else if.

    I see a bunch o' syntax errors.

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

    Default Re: Question about else if.

    Also, don't use == to compare Strings. Use the equals() method instead. Do a search on google or in these forums, this has been explained countless times before.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    tkelley1620 is offline Member
    Join Date
    May 2010
    Location
    Pittsburgh, PA
    Posts
    10
    Rep Power
    0

    Default Re: Question about else if.

    Thanks for the help I have tried to use some of your suggestions but still coming up with errors..
    "syntax error with tokens delete tokens all on the else if statements..

    import javax.swing.JOptionPane;


    public class assignment3b {


    public static void main(String[] args) {
    String name ="";
    String Circle = null;
    String radius;
    double pi = 0;
    double carea;
    String Rectangle = null;
    String base;
    String height;
    double rarea = 0;
    String Right_Triangle = null;
    String base1;
    String height1;
    double rtarea = 0;
    String Square = null;
    String base2;
    double sarea;
    int x = 5;


    // start loop
    do{

    name = JOptionPane.showInputDialog(
    null, " Please enter type of Shape: Circle, Rectangle, Right_Triangle or Square");

    // Check conditions if Circle, Rectangle, so on..
    if (name.equals(Circle));
    {
    radius = JOptionPane.showInputDialog(
    null," Please enter the radius of the circle");
    double r1 = Double.parseDouble(radius);
    carea = (pi * r1 * r1);
    }

    // check to see if input was rectangle
    else if {
    (name.equals("Rectangle"))

    base = JOptionPane.showInputDialog(
    null, "Please Enter the base of the Rectangle");
    double b = Double.parseDouble(base);
    height = JOptionPane.showInputDialog(
    "Please enter the Height of the Rectangle");
    double h = Double.parseDouble(height);
    rarea = (b * h);
    }
    // check to see if input was Right_Triangle

    else if (name.equals ("Right_Triangle"));
    {
    base1 = JOptionPane.showInputDialog(
    null, "Please Enter the Base of the Right Triangle");
    double b1 =Double.parseDouble(base1);
    height1 = JOptionPane.showInputDialog(
    "Please Enter the Height of the Right Triangle");
    double h1 = Double.parseDouble(height1);
    rtarea = (b1 * h1)/2;
    }
    // Check to see if input was square

    else if (name.equals ("Square"))

    {
    base2 = JOptionPane.showInputDialog(
    null, "Please Enter the Base of the Square");
    double b2 = Double.parseDouble(base2);
    sarea = (b2 * b2);
    while (x < 6);
    }

    }
    }
    }
    }
    }

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,932
    Rep Power
    8

    Default Re: Question about else if.

    Please use code tags when posting code!

    You have not followed our directions- you still cancel your loop/while/if statements by ending them with a semicolon.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    tkelley1620 is offline Member
    Join Date
    May 2010
    Location
    Pittsburgh, PA
    Posts
    10
    Rep Power
    0

    Default Re: Question about else if.

    actually I finally got it... Thank you so much for you help..
    just have one more question.. As you can see i have to run this loop 5 times around .. I now have to have a runing total for each of the areas.. Circle, rectangle, Right_Triangle, and Square.. and then have a final total.. thank you for your help again..

  8. #8
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,932
    Rep Power
    8

    Default Re: Question about else if.

    Quote Originally Posted by tkelley1620 View Post
    actually I finally got it... Thank you so much for you help..
    just have one more question.. As you can see i have to run this loop 5 times around .. I now have to have a runing total for each of the areas.. Circle, rectangle, Right_Triangle, and Square.. and then have a final total.. thank you for your help again..
    I'm not sure what your question actually is?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    tkelley1620 is offline Member
    Join Date
    May 2010
    Location
    Pittsburgh, PA
    Posts
    10
    Rep Power
    0

    Default Re: Question about else if.

    Keith Thank you for all your help.. It greatly appreacted.
    Hopefully this clears things up a bit.

    I have to run the program though a loop 5 times
    Then I have to print out everything that I have collected each time that I have gone though the loop.
    Then I have to pirnt out what Shape has the highest area and the name of the shape. This is what i do not know how to do...
    I have posted new code.
    #
    import javax.swing.JOptionPane;


    public class assignment3b {


    public static void main(String[] args) {
    String name ="";
    String Circle = null;
    String radius;
    double pi = 0;
    double carea;
    String Rectangle = null;
    String base;
    String height;
    double rarea = 0;
    String Right_Triangle = null;
    String base1;
    String height1;
    double rtarea = 0;
    String Square = null;
    String base2;
    double sarea;
    int x = 5;


    // start loop
    do{

    name = JOptionPane.showInputDialog(
    null, " Please enter type of Shape: Circle, Rectangle, Right_Triangle or Square");

    // Check conditions if Circle, Rectangle, so on..
    if (name.equals(Circle))
    {
    radius = JOptionPane.showInputDialog(
    null," Please enter the radius of the circle");
    double r1 = Double.parseDouble(radius);
    carea = (pi * r1 * r1);
    // Print out Statements
    System.out.println (" The area of a " + "" + name );
    System.out.println (" The radius of a " + "" + name + " is " +"" + r1);
    System.out.println ("The area of a " + "" + name + " is " + "" + carea);
    }

    // check to see if input was rectangle
    if (name.equals("Rectangle"))
    {
    base = JOptionPane.showInputDialog(
    null, "Please Enter the base of the Rectangle");
    double b = Double.parseDouble(base);
    height = JOptionPane.showInputDialog(
    "Please enter the Height of the Rectangle");
    double h = Double.parseDouble(height);
    rarea = (b * h);
    // Printout Statements
    System.out.println (" The area of a " + "" + "" + name);
    System.out.println ("The Base of" + "" +name + " is "+ "" + b);
    System.out.println ("The Height of the " + "" + name + " is"+ "" +h);
    System.out.println ("The area of a" + "" + name + "is " + "" + rarea);
    }
    // check to see if input was Right_Triangle
    if (name.equals ("Right_Triangle"))
    {
    base1 = JOptionPane.showInputDialog(
    null, "Please Enter the Base of the Right Triangle");
    double b1 =Double.parseDouble(base1);
    height1 = JOptionPane.showInputDialog(
    "Please Enter the Height of the Right Triangle");
    double h1 = Double.parseDouble(height1);
    rtarea = (b1 * h1)/2;
    // Printout Statments.
    System.out.println (" The area of a "+ "" +name);
    System.out.println ("The Base of a "+ " " +name+ ""+ "is" +base1);
    System.out.println ("The Height of a" +""+name + ""+"is" + "" +height1);
    System.out.println ("The area of a " + "" +name + "is" + "" +rtarea);
    }
    // Check to see if input was square
    if (name.equals ("Square"))
    {
    base2 = JOptionPane.showInputDialog(
    null, "Please Enter the Base of the Square");
    double b2 = Double.parseDouble(base2);
    sarea = (b2 * b2);
    // Printout Statements.
    System.out.println (" The area of a"+ "" +name);
    System.out.println (" The base of a"+ "" +name + ""+ "is" +b2);
    System.out.println (" The area of a"+ "" +name + ""+ "is" +sarea);
    }

    } while (x < 6);

    }
    }
    #
    I have po

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Question about else if.

    "Keith"??

    And you still haven't used [code] tags [/code].
    KevinWorkman likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  11. #11
    tkelley1620 is offline Member
    Join Date
    May 2010
    Location
    Pittsburgh, PA
    Posts
    10
    Rep Power
    0

    Default Re: Question about else if.

    Sorry about the name thing.. my daughter walked in and was talking about someone in school...
    here is the code...
    Java Code:
    import javax.swing.JOptionPane;
    
    
    public class assignment3b {
    
    
    	public static void main(String[] args) {
    		String name ="";
    		String Circle = null;
    		String radius;
    		double pi = 0;
    		double carea;
    		String Rectangle = null;
    		String base;
    		String height;
    		double rarea = 0;
    		String Right_Triangle = null;
    		String base1;
    		String height1;
    		double rtarea = 0;
    		String Square = null;
    		String base2;
    		double sarea;
    		int x = 5;
    				
    		
    		// start loop 
    		do{
    			
    		name = JOptionPane.showInputDialog(
    				null, " Please enter type of Shape: Circle, Rectangle, Right_Triangle or Square");
    		
    		// Check conditions if Circle, Rectangle, so on.. 
    		if (name.equals(Circle))
    		{
    	    radius = JOptionPane.showInputDialog(
    	    		null," Please enter the radius of the circle");
    	    double r1 = Double.parseDouble(radius);
    	    carea = (pi * r1 * r1);
    	    // Print out Statements 
    	    System.out.println (" The area of a " + "" + name );
    	    System.out.println (" The radius of a " + "" + name  + " is " +"" + r1);
    	    System.out.println ("The area of a " + "" + name + " is " + "" + carea); 
    		}
    		
    	    // check to see if input was rectangle 
    		 if (name.equals("Rectangle"))
    		 {
    		 base = JOptionPane.showInputDialog(
    				 null, "Please Enter the base of the Rectangle");
    		 double b = Double.parseDouble(base);
    		 height = JOptionPane.showInputDialog(
    				 "Please enter the Height of the Rectangle");
    		 double h = Double.parseDouble(height);
    		 rarea = (b * h);
    		 // Printout Statements 
    		 System.out.println (" The area of a " + "" + "" + name);
    		 System.out.println ("The Base of" + "" +name + " is "+ "" + b);
    		 System.out.println ("The Height of the " + "" + name + " is"+ "" +h);
    		 System.out.println ("The area of a" + "" + name + "is " + "" + rarea);		 		 
    		}
    		 // check to see if input was Right_Triangle		 
    		if (name.equals ("Right_Triangle"))
    		{
    		base1 = JOptionPane.showInputDialog(
    				null, "Please Enter the Base of the Right Triangle");
    		double b1 =Double.parseDouble(base1);
    		height1 = JOptionPane.showInputDialog(
    				"Please Enter the Height of the Right Triangle");
    		double h1 = Double.parseDouble(height1);
    		rtarea = (b1 * h1)/2;
    		// Printout Statments. 
    		System.out.println (" The area of a "+ ""  +name);
    		System.out.println ("The Base of a "+ " " +name+  ""+ "is" +base1);
    		System.out.println ("The Height of a" +""+name + ""+"is" + "" +height1);
    		System.out.println ("The area of a " + "" +name + "is" + "" +rtarea);		
    		}
    		// Check to see if input was square		
    	 if (name.equals ("Square"))
    		{
    		 base2 = JOptionPane.showInputDialog(
    				 null, "Please Enter the Base of the Square");
    		 double b2 = Double.parseDouble(base2);
    		 sarea = (b2 * b2);
    		 // Printout Statements. 
    		 System.out.println (" The area of a"+ "" +name);
    		 System.out.println (" The base of a"+ "" +name + ""+ "is" +b2);
    		 System.out.println (" The area of a"+ "" +name + ""+  "is" +sarea);
    		}
    	 
    		} while (x < 6);
    	
    		}
    }

  12. #12
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,932
    Rep Power
    8

    Default Re: Question about else if.

    What errors are you getting now?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  13. #13
    MonkeyMan is offline Member
    Join Date
    Jan 2012
    Posts
    45
    Rep Power
    0

    Default Re: Question about else if.

    Aaaargh! my eyes! they burn!

    Sorry, I would just like to pitch in, that it would be a lot easier for you, me and everyone else if you used indentation and spacing efficiently. Use empty lines, use tab. Easier to read, easier to spot syntax errors. Thanks :)

  14. #14
    F.S. is offline Member
    Join Date
    Feb 2012
    Location
    Bruges, BELGIUM
    Posts
    16
    Rep Power
    0

    Default Re: Question about else if.

    Don' you create an infinite loop there? Because you want the loop to keep running as long as x<6, but it starts on value 5 and you never increase it.

    The most logical thing to do would be to:
    -start x at value 0
    -add x++ (this increases x with 1) at the end of the loop
    -keep it running as long as x<5

    f.e.:

    Java Code:
    int x=0
    
    do{
        ...
        x++;
    }while(x<5);
    Last edited by F.S.; 03-14-2012 at 07:08 PM.

  15. #15
    tkelley1620 is offline Member
    Join Date
    May 2010
    Location
    Pittsburgh, PA
    Posts
    10
    Rep Power
    0

Similar Threads

  1. Java Question [Beginner Question]
    By joker760 in forum New To Java
    Replies: 3
    Last Post: 12-13-2011, 04:01 PM
  2. question posted by indissa: library question.
    By Fubarable in forum New To Java
    Replies: 2
    Last Post: 11-18-2011, 01:14 AM
  3. Question concerning question marks and colons
    By jim01 in forum New To Java
    Replies: 17
    Last Post: 01-14-2011, 12:05 AM
  4. Question mark colon operator question
    By orchid in forum Advanced Java
    Replies: 9
    Last Post: 12-19-2010, 08:49 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
  •