Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: help creating program for area and perimeter of triangle

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default help creating program for area and perimeter of triangle

    import java.lang.Math;
    public class triangle
    {
       double area; double s;
       public double findArea(double sideA, double sideB, double sideC);
    {   
          s= 0.5 *(sideA + sideB + sideC);
          area = Math.sqrt(s*(s-sideA)*(s-sideB)*(s-sideC)); 
          System.out.println("The area of the triangle is " + area);
          return area;
       }     
    }
    import java.util.Scanner
     
       public static void main(String[] args) {
     
          Formula triangle = new Formula();
          double a,b,c;
     
          Scanner inputTriangle = new Scanner(System.in);
          System.out.println("Enter side a of triangle");
          a = inputTriangle.nextDouble();
          System.out.println("Enter side b of triangle");
          b = inputTriangle.nextDouble();
          System.out.println("Enter side c of triangle");
          c = inputTriangle.nextDouble();
          triangle.findArea(a, b, c);
       }      
    }
    I've tried for hours to fix the errors in this simple program for a class I am taking. Help would be appreciated.

    Errors I am getting:

    Triangle.java:23: error: class, interface, or enum expected
    import java.util.Scanner
    ^
    Triangle.java:25: error: class, interface, or enum expected
    public static void main(String[] args) {
    ^
    Triangle.java:28: error: class, interface, or enum expected
    double a,b,c;
    ^
    Triangle.java:30: error: class, interface, or enum expected
    Scanner inputTriangle = new Scanner(System.in);
    ^
    Triangle.java:31: error: class, interface, or enum expected
    System.out.println("Enter side a of triangle");
    ^
    Triangle:32: error: class, interface, or enum expected
    a = inputTriangle.nextDouble();
    ^
    Triangle.java:33: error: class, interface, or enum expected
    System.out.println("Enter side b of triangle");
    ^
    Triangle:34: error: class, interface, or enum expected
    b = inputTriangle.nextDouble();
    ^
    Triangle:35: error: class, interface, or enum expected
    System.out.println("Enter side c of triangle");
    ^
    Triangle.java:36: error: class, interface, or enum expected
    c = inputTriangle.nextDouble();
    ^
    Triangle.java:37: error: class, interface, or enum expected
    triangle.findArea(a, b, c);
    ^
    Triangle.java:38: error: class, interface, or enum expected
    }
    ^
    12 errors
    Last edited by tinker99; September 1st, 2014 at 04:53 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: help creating program for area and perimeter of triangle

    the errors in this simple program
    Please copy the full text of the error message and paste it here. It has important info about the error.

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE GOES HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: help creating program for area and perimeter of triangle

    Please follow Java's naming conventions and start your class names with capital letters.

    The class triangle has a close brace, '}', before the method main(), there is an import statement in the middle of your code, and the method findArea() ends with the semicolon at the end of the signature. Indent properly, reorganize your code, remove incorrect semicolons/add needed ones, enclose the main() method in the class, and you should be well on your way. What is Formula?

  4. #4
    Junior Member
    Join Date
    Sep 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help creating program for area and perimeter of triangle

    Quote Originally Posted by GregBrannon View Post
    Please follow Java's naming conventions and start your class names with capital letters.

    The class triangle has a close brace, '}', before the method main(), there is an import statement in the middle of your code, and the method findArea() ends with the semicolon at the end of the signature. Indent properly, reorganize your code, remove incorrect semicolons/add needed ones, enclose the main() method in the class, and you should be well on your way. What is Formula?
    I went ahead and reorganized and simplified, but I am still having some problems. Here is the new code:

    import java.util.Scanner;
     
    public class Triangle {
       public static void main (String[] args) {
           Scanner Console = new Scanner(System.in);
           System.out.print("Please enter the three lengths of your Triangle: ");
           double a = input.nextDouble();
           double b = input.nextDouble();
           double c = input.nextDouble();
           System.out.println("Your three triangle lengths are: " + '\n' + a + '\n' + b + '\n' + c);
           System.out.println("The area of your triangle is: " + getArea(a,b,c));
     
       public static double triangleArea(double a, double b, double c) { 
          double s = (a + b + c)/2.0;
          double x = ((s)*(s-a)*(s-b)*(s-c));
          double area = Math.sqrt(x);
          return area;
       }      
    }
    and here are the errors:

    Triangle.java:23: error: illegal start of expression
    public static double triangleArea(double a, double b, double c) {
    ^
    Triangle.java:23: error: illegal start of expression
    public static double triangleArea(double a, double b, double c) {
    ^
    Triangle.java:23: error: ';' expected
    public static double triangleArea(double a, double b, double c) {
    ^
    Triangle.java:23: error: '.class' expected
    public static double triangleArea(double a, double b, double c) {
    ^
    Triangle.java:23: error: ';' expected
    public static double triangleArea(double a, double b, double c) {
    ^
    Triangle.java:23: error: <identifier> expected
    public static double triangleArea(double a, double b, double c) {
    ^
    Triangle.java:23: error: not a statement
    public static double triangleArea(double a, double b, double c) {
    ^
    Triangle.java:23: error: ';' expected
    public static double triangleArea(double a, double b, double c) {
    ^
    Triangle.java:29: error: reached end of file while parsing
    }
    ^

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: help creating program for area and perimeter of triangle

    It looks like there is a problem with the pairing of the {}s
    Make sure the code for every method is enclosed within {}s.
    One method can NOT be inside of another.

    It might help to see the pairing if the ending } for a method has a label;
        } //  end main()

    Also the ending/pairing } should be in the same column as the first character on the line with the { that goes with it:
         if(x > 1 ) {
            //  something
         }  //  this is under the i in if
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: help creating program for area and perimeter of triangle

    Also posted at: Area Of Triangle Java Program Help? - Java | Dream.In.Code
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [Easy] Finding area of triangle, rectangle, and circle.
    By GMPoison in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 3rd, 2013, 06:21 PM
  2. Creating button from image and selecting clickable area
    By startas in forum AWT / Java Swing
    Replies: 1
    Last Post: September 3rd, 2013, 09:48 AM
  3. Diamond perimeter with loop
    By Lanto in forum Loops & Control Statements
    Replies: 5
    Last Post: August 9th, 2012, 12:02 PM
  4. Triangles problem - creating triangle objects
    By sparky5783 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: May 15th, 2011, 12:38 PM
  5. Area of a triangle (using 2 extra methods) error help
    By SilentPirate in forum What's Wrong With My Code?
    Replies: 9
    Last Post: September 12th, 2010, 06:08 PM