1st java program -- missing a { ???

Discussion in 'Mac Programming' started by mcmacmcmac, Oct 10, 2007.

  1. mcmacmcmac macrumors member

    Joined:
    Oct 2, 2007
    #1
    My first java program.. I keep getting this error msg when I compile..

    '{' expected
    public class AreaOfShapes.java
    ^
    1 error

    Tool completed with exit code 1

    But I don't think I'm missing a { .. Can someone tell from this:

    import java.util.Scanner; // program uses class Scanner


    public class AreaOfShapes2.java
    {

    public static void main( String args[] )
    {

    System.out.println("Student Name: Bob\nStudent ID: 999\n\n");

    // create scanner object to obtain input from user
    Scanner input = new Scanner(System.in);

    // display choices of shapes for the user to choose from
    System.out.println("Choose one of the following options: \n1 for calculating the area of a circle");
    System.out.print("2 for calculating the area of a square\n3 for calculating the area of a rectangle\nEnter your choice:");
    int choice = input.nextInt(); // read the choice from user


    if (choice > 3) // display an error message indicating an invalid selection
    System.out.println("Your selection is invalid. Please try again.");
    else if (choice == 1) // calculate the area of a circle
    {
    System.out.print("\nEnter the radius of the circle:");
    double radius = input.nextDouble();
    double area = (3.14 * radius * radius);
    System.out.printf("The area of the circle is %.2f\n", area);
    }
    else if (choice == 2) // calculate the area of a square
    {
    System.out.print("\nEnter the length of a side of the square:");
    double side = input.nextDouble();
    double area = (side * side);
    System.out.printf("The area of the square is %.2f\n", area);
    }
    else if (choice == 3) // calculate the area of a rectangle
    {
    System.out.print("\nEnter the length of the rectangle: ");
    double length = input.nextDouble();
    System.out.print("Enter the width of the rectangle: ");
    double width = input.nextDouble();
    double area = (width * length);
    System.out.printf("The area of the rectangle is %.2f\n", area);
    }

    } // end method Main

    } // end class AreaOfShapes2
     
  2. italiano40 macrumors 65816

    italiano40

    Joined:
    Oct 7, 2007
    Location:
    NY
    #2
    add a "}" at the end of the program and it should work

    :apple:

    and next time use
    Code:
    to show code
     
  3. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #3
    That's impossible. "AreaOfShapes" is not even referenced in that file.

    Typo? Compiling the wrong file?
     
  4. mcmacmcmac thread starter macrumors member

    Joined:
    Oct 2, 2007
    #4
    that didn't work but i figured it out.

    i put .java at the end of line below

    public class AreaOfShapes2.java



    Thank you anways
     
  5. italiano40 macrumors 65816

    italiano40

    Joined:
    Oct 7, 2007
    Location:
    NY
    #5
    remove .java on the end of the class

    i ran it in bluej and it works fine
     
  6. mcmacmcmac thread starter macrumors member

    Joined:
    Oct 2, 2007
    #6

    yup i just noticed that.. thanks very much!
     
  7. italiano40 macrumors 65816

    italiano40

    Joined:
    Oct 7, 2007
    Location:
    NY

Share This Page