Results 1 to 17 of 17
Thread: Java program error
 06062011, 04:28 AM #1Member
 Join Date
 May 2011
 Posts
 29
 Rep Power
 0
Java program error
i want to use at least 2 methods
i want to write a program that inputs 3 integers, which are sides of a triangle
and determines the type of triangle it is:
isosceles
equilateral
scalene
not valid sides of a triangle
If it is a valid triangle print the type of triangle it is, its area and perimeter.
the program will then repeat for another set of sides, until a 0 is entered for the 1st side of the triangle (the other 2 sides should not be read in for this case)
This is what i got so far:
Java Code:import java.util.*; public class BookExample{ public static void main(String[] args) {int a, b, c, p; System.out.println("Enter the length of side \"a\" of the triangle to begin:"); Scanner in=new Scanner(System.in); a=in.nextInt(); if(a==0) System.exit(0); b=in.nextInt(); c=in.nextInt(); getTriType(); getArea(); p = a+b+c; } public static void getTriType() {if(a==0b==0c==0a<0b<0c<0a+b>=cb+c>=aa+c>=b) {System.out.println("According to the given sides these are invalid sides of a Triangle"); } else if(a==b&&a==c&&b==c) {System.out.print("According to the given sides, since a,b and c are equal "); System.out.print("the triangle is an Equilateral triangle."); } else if(a==ba==cb==ab==cc==ac==b) {System.out.print("According to the given sides, since two sides are equal "); System.out.print("the triangle is an Isosceles triangle."); } else {System.out.print("According to the given sides, since a,b and c are not equal "); System.out.print("the triangle is a Scalene triangle."); } } public static double getArea(int a,int b,int c) {double s = 0.5 * (a + b + c); double area = Math.sqrt(s*(sa)*(sb)*(sc)); } }
 06062011, 04:47 AM #2
 06062011, 04:49 AM #3Member
 Join Date
 Jan 2011
 Posts
 23
 Rep Power
 0
You need to make the parameters of the function definition the same as the parameters in the function call within your main.
getArea(); is not the same as
public static double getArea(int a, int b, int c)
when you call getArea from main, make sure you include the values that you inputted from the console.
 06062011, 04:55 AM #4Member
 Join Date
 May 2011
 Posts
 29
 Rep Power
 0
 06062011, 04:58 AM #5Member
 Join Date
 Jan 2011
 Posts
 23
 Rep Power
 0
 06062011, 05:02 AM #6
 06062011, 05:02 AM #7Member
 Join Date
 May 2011
 Posts
 29
 Rep Power
 0
Thanks. I knew more errors were going to follow up right behind that one. Now i'm getting something like "BookExample.java:16: '.class' expected
getArea(int a,int b,int c);"
 06062011, 05:05 AM #8
/me predicts the future.
 06062011, 05:11 AM #9Member
 Join Date
 May 2011
 Posts
 29
 Rep Power
 0
I see what your talking about i changed it and just called the variables instead and did the same to my other function which began getting an error that said something like 'can't find a' etc...(a,b,c); now my error is completely different.
Java Code:import java.util.*; public class BookExample{ public static void main(String[] args) {int a, b, c, p; System.out.println("Enter the length of side \"a\" of the triangle to begin:"); Scanner in=new Scanner(System.in); a=in.nextInt(); if(a==0) System.exit(0); b=in.nextInt(); c=in.nextInt(); getTriType(a,b,c); getArea(a,b,c); p = a+b+c; } public static void getTriType(int a, int b, int c) {if(a==0b==0c==0a<0b<0c<0a+b>=cb+c>=aa+c>=b) {System.out.println("According to the given sides these are invalid sides of a Triangle"); } else if(a==b&&a==c&&b==c) {System.out.print("According to the given sides, since a,b and c are equal "); System.out.print("the triangle is an Equilateral triangle."); } else if(a==ba==cb==ab==cc==ac==b) {System.out.print("According to the given sides, since two sides are equal "); System.out.print("the triangle is an Isosceles triangle."); } else {System.out.print("According to the given sides, since a,b and c are not equal "); System.out.print("the triangle is a Scalene triangle."); } } public static double getArea(int a,int b,int c) {double s = 0.5 * (a + b + c); double area = Math.sqrt(s*(sa)*(sb)*(sc)); } }
}
^"
 06062011, 05:12 AM #10Member
 Join Date
 May 2011
 Posts
 29
 Rep Power
 0
 06062011, 05:13 AM #11Member
 Join Date
 Jan 2011
 Posts
 23
 Rep Power
 0
 06062011, 05:16 AM #12
What does that error message suggest to you? I think it is telling you that there is supposed to be a return statement but it is missing. Do you have any methods that SHOULD have a return statement but DON'T?
 06062011, 05:23 AM #13Member
 Join Date
 May 2011
 Posts
 29
 Rep Power
 0
Being that i'm new to java i'm actually lost right now.
 06062011, 05:27 AM #14Java Code:
public static double getArea(int a,int b,int c)
 06062011, 05:39 AM #15Member
 Join Date
 May 2011
 Posts
 29
 Rep Power
 0
 06062011, 05:42 AM #16Member
 Join Date
 Jan 2011
 Posts
 23
 Rep Power
 0
The method is named "getArea". You don't want to change it to void or it won't "get" anything.
You want that method to return a value of type double which is the area of the triangle.
Returning a Value from a Method (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
 06062011, 05:59 AM #17Member
 Join Date
 May 2011
 Posts
 29
 Rep Power
 0
Similar Threads

error in java program..
By mudit222 in forum New To JavaReplies: 2Last Post: 12172010, 10:32 AM 
Error running java program using URL
By gio123bg in forum New To JavaReplies: 6Last Post: 06302009, 06:26 PM 
error running java program
By bdasilva in forum New To JavaReplies: 1Last Post: 06292009, 01:46 AM 
Same error msg with every program Java Textpad
By peterdfl in forum New To JavaReplies: 7Last Post: 04172009, 04:49 AM 
How to get error codes using java program
By kasipandian in forum Web FrameworksReplies: 10Last Post: 05252008, 05:00 PM
Bookmarks