posted 10 years ago

Ok im new to java like most with issues on this thread, so all my code is kinda a work in progress anyway, however i have a specific issue that I need addressed.

Im doing an assignment that involves displaying info about the coordinates of a triangle. I have to take 3 coordinates and calc the perimeter/area and display them and such. Theres other parts i have not gotten to yet such as adding an accessor and mutator to each method for the points, area and circumference.

I am getting a single error message that is keeping me from compiling.

Triangle.java:56: ';' expected

}

^

Ill post the code below, but for reference, the error is directly after return triangle.

public class Triangle

{

public Triangle(double x1, double y1, double x2, double y2, double x3, double y3)

{

/** Define 2-dimensional coordinate points that form

a triangle in double precision floating-point numbers

for values such that

Setting (x1,y1) = (0,0), (x2,y2) = (1,0) and (x3,y3) = (1,1)

*/

double x1 = 0;

double y1 = 0;

double x2 = 1;

double y2 = 0;

double x3 = 1;

double y3 = 1;

}

public double dist12()

/** Distance formula calulation from verteces of the Triangle:

dist = (Math.sqrt(a2 - a1) * (a2 - a1) + (b2 - b1) * (b2 - b1))

where (a1,b1) and (a2, b2) are points that make up a side of a triangle

*/

{

dist12 = (Math.sqrt(x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));

}

public double dist23()

{

dist23 = (Math.sqrt(x3 - x2) * (x3 - x2) + (y3 - y2) * (y3 - y2));

}

public double dist13()

{

dist13 = (Math.sqrt(x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3));

}

/** Circumference calculation by the formula(Circumference = permimeter)

Circumference = side1 + side2 + side3

*/

public double circumference( double dist12, double dist23, double dist13)

{

circumference = dist12 + dist23 + dist13;

}

// Area calculation by the formula of Heron's formula

public double area( double circumference, double dist12, double dist23, double dist13)

{

area = (Math.sqrt(((circumference * 0.5)) * ((circumference * 0.5) - dist12) * ((circumference * 0.5) - dist23) * ((circumference * 0.5) - dist13)));

}

public String toString()

{

return Triangle

}

}

Im doing an assignment that involves displaying info about the coordinates of a triangle. I have to take 3 coordinates and calc the perimeter/area and display them and such. Theres other parts i have not gotten to yet such as adding an accessor and mutator to each method for the points, area and circumference.

I am getting a single error message that is keeping me from compiling.

Triangle.java:56: ';' expected

}

^

Ill post the code below, but for reference, the error is directly after return triangle.

public class Triangle

{

public Triangle(double x1, double y1, double x2, double y2, double x3, double y3)

{

/** Define 2-dimensional coordinate points that form

a triangle in double precision floating-point numbers

for values such that

Setting (x1,y1) = (0,0), (x2,y2) = (1,0) and (x3,y3) = (1,1)

*/

double x1 = 0;

double y1 = 0;

double x2 = 1;

double y2 = 0;

double x3 = 1;

double y3 = 1;

}

public double dist12()

/** Distance formula calulation from verteces of the Triangle:

dist = (Math.sqrt(a2 - a1) * (a2 - a1) + (b2 - b1) * (b2 - b1))

where (a1,b1) and (a2, b2) are points that make up a side of a triangle

*/

{

dist12 = (Math.sqrt(x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));

}

public double dist23()

{

dist23 = (Math.sqrt(x3 - x2) * (x3 - x2) + (y3 - y2) * (y3 - y2));

}

public double dist13()

{

dist13 = (Math.sqrt(x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3));

}

/** Circumference calculation by the formula(Circumference = permimeter)

Circumference = side1 + side2 + side3

*/

public double circumference( double dist12, double dist23, double dist13)

{

circumference = dist12 + dist23 + dist13;

}

// Area calculation by the formula of Heron's formula

public double area( double circumference, double dist12, double dist23, double dist13)

{

area = (Math.sqrt(((circumference * 0.5)) * ((circumference * 0.5) - dist12) * ((circumference * 0.5) - dist23) * ((circumference * 0.5) - dist13)));

}

public String toString()

{

return Triangle

}

}

posted 10 years ago

Welcome to JavaRanch!

The error message is telling you that a semicolon is expected on line 56 of your code. Adding a semicolon to the line above it would make the syntax reasonable...

return Triangle;

Unfortunately, this will open the door to a lot more errors (36, actually), most of which are problems with variable declaration: Either trying to define the same variable more than once, or trying to use a variable outside of its defined scope.

[ February 18, 2008: Message edited by: marc weber ]

Originally posted by Daniel Martin:

... I am getting a single error message that is keeping me from compiling.

Triangle.java:56: ';' expected...

Welcome to JavaRanch!

The error message is telling you that a semicolon is expected on line 56 of your code. Adding a semicolon to the line above it would make the syntax reasonable...

return Triangle;

Unfortunately, this will open the door to a lot more errors (36, actually), most of which are problems with variable declaration: Either trying to define the same variable more than once, or trying to use a variable outside of its defined scope.

[ February 18, 2008: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." *~Joe Strummer*

sscce.org

posted 10 years ago

Interesting... Just by a quick glance, I don't see how you are getting a "single error message". There should be dozens of errors !!

Can you delete the class files and recompile the code?

[EDIT: Thanks Marc. I totally forgot that this could be a case of one error masking dozens of errors.]

Henry

[ February 18, 2008: Message edited by: Henry Wong ]

I am getting a single error message that is keeping me from compiling.

Triangle.java:56: ';' expected

}

^

Interesting... Just by a quick glance, I don't see how you are getting a "single error message". There should be dozens of errors !!

Can you delete the class files and recompile the code?

[EDIT: Thanks Marc. I totally forgot that this could be a case of one error masking dozens of errors.]

Henry

[ February 18, 2008: Message edited by: Henry Wong ]

Reyn Marc

Greenhorn

Posts: 2

posted 10 years ago

ok well just as was stated when i added the ";" a whole slew of errors popped up. i removed the 2nd set of declarations of double, however I am still left with 20 or so errors all of which are cannot find symbol in different lines. What might be me problem now, and more importantly I suppose, why would these errors not have come up before?

EDIT: i see now your edit about it masking errors, just not sure I understand if the errors were always there, why it wouldn't see them.

[ February 18, 2008: Message edited by: Reyn Marc ]

EDIT: i see now your edit about it masking errors, just not sure I understand if the errors were always there, why it wouldn't see them.

[ February 18, 2008: Message edited by: Reyn Marc ]

posted 10 years ago

Sometimes, as in this case, the one error confuses the parser enough that it can't make any sense of the rest of the code. rather than make guesses as to what you mean, it simply bails out, effectively saying "I found this error, and couldn't understand anything else".

This is a good lesson for you, though. If this is the first time you've compiled, my advice would be: slow down. When I write code, I try to not write more than 2-3 lines before recompiling - sometimes only one line. That way, you've not gonna get 36 errors all at once. you'll get one or two, and you know exactly what changed so you can focus your investigation on those lines to fix the problem.

This is a good lesson for you, though. If this is the first time you've compiled, my advice would be: slow down. When I write code, I try to not write more than 2-3 lines before recompiling - sometimes only one line. That way, you've not gonna get 36 errors all at once. you'll get one or two, and you know exactly what changed so you can focus your investigation on those lines to fix the problem.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

posted 10 years ago

Think of it this way. The compiler is a program designed to find errors. It's really good at it, but with each error, your program is not really a valid Java program anymore. There will be certain errors where the compiler can't recover correctly.

In some cases, it will mask other errors. In other cases, it will indicate errors where there isn't any. This is why, you either have compile errors, or you don't -- the count isn't relevant. This is why, you generally fix the first few errors, and then recompile -- as the later ones may not make sense.

Henry

just not sure I understand if the errors were always there, why it wouldn't see them.

Think of it this way. The compiler is a program designed to find errors. It's really good at it, but with each error, your program is not really a valid Java program anymore. There will be certain errors where the compiler can't recover correctly.

In some cases, it will mask other errors. In other cases, it will indicate errors where there isn't any. This is why, you either have compile errors, or you don't -- the count isn't relevant. This is why, you generally fix the first few errors, and then recompile -- as the later ones may not make sense.

Henry