Results 1 to 9 of 9
- 02-21-2009, 06:07 PM #1
Member
- Join Date
- Feb 2009
- Posts
- 42
- Rep Power
- 0
[SOLVED] I need help fixing an error
Can anyone help me fix my error, the error occurs in the red line
import javax.swing.JOptionPane;
public class ShapesCalc {
public static void main(String[] args) {
//Prompt the user to Enter S, C, or T
String shapeString = JOptionPane.showInputDialog("Enter S, C, or T: ");
char shape = line.charAt(shapeString);
switch(shape) {
case 's':;
case 'S': {
String lengthString = JOptionPane.showInputDialog("Enter length of side: ");
double length = Double.parseDouble(lengthString);
//Calculate perimeter and area of square
double perimeter= ( 4 * length);
double area=(length*length);
String output = "Perimeter is equal to: " + perimeter + "\n" + "Area is equal to: " + area;
JOptionPane.showMessageDialog(null,output);
}; break;
case 'c':;
case 'C': {
String radiusString = JOptionPane.showInputDialog("Enter radius: ");
double radius = Double.parseDouble(radiusString);
//Calculate circumference and area of circle
double circumference= (2*3.14*radius);
double area = (Math.PI * (radius * radius));
String output2 ="Circumference is equal to: " + circumference + "\n" + "Area is equal to: " + area;
JOptionPane.showMessageDialog(null,output2);
}; break;
case 't':;
case 'T': {
String baseString = JOptionPane.showInputDialog("Enter length of base: ");
double base = Double.parseDouble(baseString);
String heightString = JOptionPane.showInputDialog("Enter height of triange: ");
double height = Double.parseDouble(heightString);
//Calculate area of triangle
double area = (base * height) / 2;
String output3 = "Area is equal to: " + area;
JOptionPane.showMessageDialog(null,output3);
}; break;
default: {
JOptionPane.showMessageDialog(null,"Incorrect variable please enter S,C, or T only");
System.exit(1);
};
}
- 02-21-2009, 06:49 PM #2
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
what's "line"? what's the error you're getting. what have you tried so far?
- 02-21-2009, 06:56 PM #3
Member
- Join Date
- Feb 2009
- Posts
- 42
- Rep Power
- 0
class expected
not a statement
i found line on another website i have no idea what it is for
I've tried
char shape = Char.parseChar(shapeString);
double shape = Double.parseDouble(shapeString);
char shape = Char.parseCharAt(shapeString);
- 02-21-2009, 06:58 PM #4
Member
- Join Date
- Feb 2009
- Posts
- 42
- Rep Power
- 0
The problem is I wrote the whole program in scanner and it worked and then I reread the instructions and found I had to use JOption so i'm having trouble converting over
- 02-21-2009, 07:18 PM #5
should be
Try that.Java Code:char shape = JOptionPane.showInputDialog("Enter S, C, or T: ").charAt(0);
-MK12Tell me if you want a cool Java logo avatar like mine and I'll make you one.
- 02-21-2009, 07:31 PM #6
Member
- Join Date
- Feb 2009
- Posts
- 42
- Rep Power
- 0
As always you are amazing!!! It worked thank you
- 02-21-2009, 08:03 PM #7
No problem. Please go to the top of the page and click Thread Tools -> Mark This Thread As Solved.
-MK12Tell me if you want a cool Java logo avatar like mine and I'll make you one.
- 02-21-2009, 08:25 PM #8
Here's a better approach then doing empty cases:
notice I made the char uppercase so the lowercase switch cases are no longer necessary. You should split this into methods though, not all in the main. I might show you in another post..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 char shape = Character.toUpperCase(JOptionPane.showInputDialog("Enter S, C, or T: ").charAt(0)); switch(shape) { 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': { 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': { 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); }; } } }
-MK12Tell me if you want a cool Java logo avatar like mine and I'll make you one.
- 02-21-2009, 08:33 PM #9
I've split it into different methods. Thats a better way of doing it.Java Code:import java.util.Scanner; import javax.swing.JOptionPane; public class ShapesCalc { public ShapesCalc() { } public static void main(String[] args) { ShapesCalc sc = new ShapesCalc(); sc.calcShape(); } private char getShape() { return Character.toUpperCase( JOptionPane.showInputDialog("Enter S, C, or T: ").charAt(0)); } private void calcShape() { Scanner input = new Scanner(System.in); double area; switch(getShape()) { 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': { 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': { 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); }; } input.close(); } }
-MK12Tell me if you want a cool Java logo avatar like mine and I'll make you one.
Similar Threads
-
help with fixing my assignment.please...error
By Jin-enigma-PL in forum New To JavaReplies: 4Last Post: 02-05-2009, 12:12 AM -
Diference Between compiler error Garbage collection and Runtime Error?
By makpandian in forum New To JavaReplies: 3Last Post: 01-23-2009, 08:53 AM -
error 530 error authentication required
By rgale in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 05-12-2008, 04:28 PM -
I need help fixing my code.. or non code?
By MrHuggykins in forum New To JavaReplies: 1Last Post: 03-19-2008, 10:12 PM -
Exception Error need help fixing
By skinnybones in forum New To JavaReplies: 2Last Post: 12-03-2007, 07:14 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks