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

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

    }

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

    Default

    why won't it compile?

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

    Default

    it has like 6,000 errors!!!

    It says:

    'else' without 'if'
    illegal start of type
    ; expected
    <identifier> expected
    class, interface, or enum expected

  4. #4
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    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.

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

    Default

    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

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

    Default

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

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

    Default

    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

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

    Default

    o and it says there is something wrong with this line

    char shape = input.nextChar();

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

    Default

    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....
    -MK12
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

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

    Default

    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);
            }
        }
    }
    Look through it and see what you did wrong.
    A better way to do this would be with a switch statement.
    -MK12
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

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

    Default

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

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

    Default

    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);
                };
            }
        }
    }
    notice I put cases for lowercase letters that did nothing, but had no break statement, so they fall through to the uppercase case, so entering lowercase letters will work too. Another way instead of putting lowercase cases would be to make the char uppercase beforehand.
    -MK12
    Last edited by MK12; 02-21-2009 at 04:09 PM.
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

Posting Permissions

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