Results 1 to 12 of 12

# Thread: [SOLVED] Please Help!!!

- 02-21-2009, 04:21 AM #1Member
- Join Date
- Feb 2009
- Posts
- 42

- Rep Power
- 0

## [SOLVED] Please Help!!!

This was my assignment for my computer science class, underneath I wrote a program for it but I can't seem to get it to compile, can anyone help :confused::

Write a program to allow the user to calculate the area and perimeter of a square, or the area and circumference of a circle, or the area of a triangle. To do this, the user will enter one of the following characters: S, C, or T. The program should then ask the user for the appropriate information in order to make the calculation, and should display the results of the calculation. See the example program execution shown in class. The program should use dialog boxes. When expecting an S, C, or T, the program should reject other characters with an appropriate message.Get extra points for allowing both the uppercase and lowercase versions of a valid character to work. Name the program ShapesCalc.java.

import java.util.Scanner;

public class ShapesCalc {

public static void main(String[] args) {

//Create a Scanner

Scanner input = new Scanner(System.in);

//Prompt the user to Enter S,C, or T

System.out.print("Enter S, C, or T: ");

char shape = input.nextChar();

if (shape == S);

System.out.print("Enter length of side: ");

double length = input.nextDouble();

//Calculate perimeter and area of square

perimeter= (4*length);

area=(length*length);

System.out.println("Perimeter is equal to: " + perimeter + "Area is equal to: " + area);

else if (shape == C);

System.out.print("Enter radius: ");

double radius = input.nextDouble();

//Calculate circumference and area of circle

circumference= (2*3.14*radius);

area2=(3.14*radius*radius);

System.out.println("Circumference is equal to: " + circumference + "Area is equal to: " + area2);}

else if (shape == T);

System.out.print("Enter length of base: ");

double base = input.nextDouble();

System.out.print("Enter height of triange: ");

double height = input.nextDouble();

//Calculate area of triangle

area3=(base*1/2*height);

System.out.println("Area is equal to: " + area3);

else

System.out.println("Incorrect variable please enter S,C, or T only");

System.exit(0);

System.out.println("Justeena Leonard");

}

}

- 02-21-2009, 04:28 AM #2Senior Member
- Join Date
- Sep 2008
- Posts
- 564

- Rep Power
- 8

why won't it compile?

- 02-21-2009, 04:30 AM #3Member
- Join Date
- Feb 2009
- Posts
- 42

- Rep Power
- 0

it has like 6,000 errors!!!

It says:

'else' without 'if'

illegal start of type

; expected

<identifier> expected

class, interface, or enum expected

- 02-21-2009, 04:39 AM #4Senior Member
- Join Date
- Sep 2008
- Posts
- 564

- Rep Power
- 8

then i suggest you correct each error line by line. the compiler, most of the time, will tell you what you need to fix, including the exact spot on the errant line. for example:

'else' without 'if' = you are using an 'else' without and 'if'. if you did this deliberately, you should start learning from scratch all over again. if you didn't, then you probably messed up your curly bracers somewhere.

matter of fact, while i haven't read your code, i'm sure that most of your errors are due to problems with your bracers.

- 02-21-2009, 04:43 AM #5Member
- Join Date
- Feb 2009
- Posts
- 42

- Rep Power
- 0

I have gone through it like by line I don't see the problem ... I am new to java and I really don't get it but i'm just trying to get through this class ... even if I wanted to figure out where I went wrong I wouldn't even know where to start

- 02-21-2009, 04:46 AM #6Senior Member
- Join Date
- Sep 2008
- Posts
- 564

- Rep Power
- 8

then remove everything and add in line by line to see what works and what doesn't.

- 02-21-2009, 04:58 AM #7Member
- Join Date
- Feb 2009
- Posts
- 42

- Rep Power
- 0

OK I deleted everything and then added everything up to the if (shape == S) and its telling me it can't find the variable S

- 02-21-2009, 05:01 AM #8Member
- Join Date
- Feb 2009
- Posts
- 42

- Rep Power
- 0

o and it says there is something wrong with this line

char shape = input.nextChar();

- 02-21-2009, 06:36 AM #9
its 'S', not S, just S and it assumes its a variable. Use single quotes for chars. "; expected" is pretty self explanatory. Another problem, you have many statements per if/else if/else but no braces, and you put a ; right after the test expression for the ifs, that means nothing will happen.. Maybe I'll try to clean it up for you....

-MK12Tell me if you want a cool Java logo avatar like mine and I'll make you one.

- 02-21-2009, 06:46 AM #10
As much as your "Help!!" title annoyed me, I took a few minutes to make it compile.

Java Code:import java.util.Scanner; public class ShapesCalc { public static void main(String[] args) { //Create a Scanner Scanner input = new Scanner(System.in); double area; //Prompt the user to Enter S, C, or T System.out.print("Enter S, C, or T: "); char shape = input.next().charAt(0); if (shape == 'S') { System.out.print("Enter length of side: "); double length = input.nextDouble(); //Calculate perimeter and area of square double perimeter= ( 4 * length); area=(length*length); System.out.println("Perimeter is equal to: " + perimeter + "Area is equal to: " + area); } else if (shape == 'C') { System.out.print("Enter radius: "); double radius = input.nextDouble(); //Calculate circumference and area of circle double circumference = (2 * 3.14 * radius); area = (Math.PI * (radius * radius)); System.out.println("Circumference is equal to: " + circumference + "Area is equal to: " + area); } else if (shape == 'T') { System.out.print("Enter length of base: "); double base = input.nextDouble(); System.out.print("Enter height of triange: "); double height = input.nextDouble(); //Calculate area of triangle area = (base * height) / 2; System.out.println("Area is equal to: " + area); } else { System.out.println("Incorrect variable please enter S,C, or T only"); System.exit(1); } } }

A better way to do this would be with a switch statement.

-MK12Tell me if you want a cool Java logo avatar like mine and I'll make you one.

- 02-21-2009, 06:58 AM #11Member
- Join Date
- Feb 2009
- Posts
- 42

- Rep Power
- 0

You are the most amazing person in the world!!!! Thank you so much!!!!

- 02-21-2009, 05:02 PM #12
Your welcome..

Here is how it would be done with a switch statement:

Java Code:import java.util.Scanner; public class ShapesCalc { public static void main(String[] args) { //Create a Scanner Scanner input = new Scanner(System.in); double area; //Prompt the user to Enter S, C, or T System.out.print("Enter S, C, or T: "); char shape = input.next().charAt(0); switch(shape) { case 's':; case 'S': { System.out.print("Enter length of side: "); double length = input.nextDouble(); //Calculate perimeter and area of square double perimeter= ( 4 * length); area=(length*length); System.out.println("Perimeter is equal to: " + perimeter + "Area is equal to: " + area); }; break; case 'c':; case 'C': { System.out.print("Enter radius: "); double radius = input.nextDouble(); //Calculate circumference and area of circle double circumference= (2*3.14*radius); area = (Math.PI * (radius * radius)); System.out.println("Circumference is equal to: " + circumference + "Area is equal to: " + area); }; break; case 't':; case 'T': { System.out.print("Enter length of base: "); double base = input.nextDouble(); System.out.print("Enter height of triange: "); double height = input.nextDouble(); //Calculate area of triangle area = (base * height) / 2; System.out.println("Area is equal to: " + area); }; break; default: { System.out.println("Incorrect variable please enter S,C, or T only"); System.exit(1); }; } } }

-MK12Last edited by MK12; 02-21-2009 at 05:09 PM.

Tell me if you want a cool Java logo avatar like mine and I'll make you one.

## Bookmarks