Results 1 to 15 of 15
Thread: Question about else if.
- 03-13-2012, 06:11 PM #1
Member
- Join Date
- May 2010
- Location
- Pittsburgh, PA
- Posts
- 10
- Rep Power
- 0
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); } } } } } }
- 03-13-2012, 06:13 PM #2
Re: Question about else if.
Yikes, check your indentation, and make sure that you don't do this:
The same thing applies to while, else, for, etc.Java Code:if(something); //that semicolon is the end of the if statement! { //always gets called }How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 03-13-2012, 06:14 PM #3
Member
- Join Date
- Mar 2012
- Posts
- 8
- Rep Power
- 0
Re: Question about else if.
I see a bunch o' syntax errors.
- 03-13-2012, 06:15 PM #4
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!
- 03-13-2012, 07:29 PM #5
Member
- Join Date
- May 2010
- Location
- Pittsburgh, PA
- Posts
- 10
- Rep Power
- 0
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);
}
}
}
}
}
}
- 03-13-2012, 07:35 PM #6
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!
- 03-13-2012, 08:03 PM #7
Member
- Join Date
- May 2010
- Location
- Pittsburgh, PA
- Posts
- 10
- Rep Power
- 0
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..
- 03-13-2012, 11:47 PM #8
Re: Question about else 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!
- 03-14-2012, 01:27 AM #9
Member
- Join Date
- May 2010
- Location
- Pittsburgh, PA
- Posts
- 10
- Rep Power
- 0
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
- 03-14-2012, 10:05 AM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
Re: Question about else if.
"Keith"??
And you still haven't used [code] tags [/code].Please do not ask for code as refusal often offends.
- 03-14-2012, 05:25 PM #11
Member
- Join Date
- May 2010
- Location
- Pittsburgh, PA
- Posts
- 10
- Rep Power
- 0
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); } }
- 03-14-2012, 05:29 PM #12
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!
- 03-14-2012, 05:43 PM #13
Member
- Join Date
- Jan 2012
- Posts
- 45
- Rep Power
- 0
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 :)
- 03-14-2012, 07:03 PM #14
Member
- Join Date
- Feb 2012
- Location
- Bruges, BELGIUM
- Posts
- 16
- Rep Power
- 0
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.
- 03-14-2012, 08:02 PM #15
Member
- Join Date
- May 2010
- Location
- Pittsburgh, PA
- Posts
- 10
- Rep Power
- 0
Similar Threads
-
Java Question [Beginner Question]
By joker760 in forum New To JavaReplies: 3Last Post: 12-13-2011, 04:01 PM -
question posted by indissa: library question.
By Fubarable in forum New To JavaReplies: 2Last Post: 11-18-2011, 01:14 AM -
Question concerning question marks and colons
By jim01 in forum New To JavaReplies: 17Last Post: 01-14-2011, 12:05 AM -
Question mark colon operator question
By orchid in forum Advanced JavaReplies: 9Last Post: 12-19-2010, 08:49 AM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks