Results 1 to 9 of 9
  1. #1
    leonardjr is offline Member
    Join Date
    Feb 2009
    Posts
    42
    Rep Power
    0

    Default [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);
    };
    }

  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    what's "line"? what's the error you're getting. what have you tried so far?

  3. #3
    leonardjr is offline Member
    Join Date
    Feb 2009
    Posts
    42
    Rep Power
    0

    Default

    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);

  4. #4
    leonardjr is offline Member
    Join Date
    Feb 2009
    Posts
    42
    Rep Power
    0

    Default

    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

  5. #5
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    should be
    Java Code:
    char shape = JOptionPane.showInputDialog("Enter S, C, or T: ").charAt(0);
    Try that.
    -MK12
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

  6. #6
    leonardjr is offline Member
    Join Date
    Feb 2009
    Posts
    42
    Rep Power
    0

    Default

    As always you are amazing!!! It worked thank you

  7. #7
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    No problem. Please go to the top of the page and click Thread Tools -> Mark This Thread As Solved.
    -MK12
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

  8. #8
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    Here's a better approach then doing empty cases:
    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);
                };
            }
        }
    }
    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..
    -MK12
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

  9. #9
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    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();
        }
    }
    I've split it into different methods. Thats a better way of doing it.
    -MK12
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

Similar Threads

  1. help with fixing my assignment.please...error
    By Jin-enigma-PL in forum New To Java
    Replies: 4
    Last Post: 02-05-2009, 01:12 AM
  2. Replies: 3
    Last Post: 01-23-2009, 09:53 AM
  3. error 530 error authentication required
    By rgale in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 05-12-2008, 05:28 PM
  4. I need help fixing my code.. or non code?
    By MrHuggykins in forum New To Java
    Replies: 1
    Last Post: 03-19-2008, 11:12 PM
  5. Exception Error need help fixing
    By skinnybones in forum New To Java
    Replies: 2
    Last Post: 12-03-2007, 08:14 PM

Posting Permissions

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