Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!

# if statement problem

Nick Hallloran
Greenhorn
Posts: 19
My solution keeps coming up as true and i cannot make it say false if it is not true. The question is to write a program that validates a triangle. Any two integers added together must be larger than the last number to be a triangle.

Rob Spoor
Sheriff
Posts: 20531
54
Consider the example where the sides are 1, 2 and 3. Since 1 + 2 == 3 this cannot form a triangle. However, 2 + 3 > 1 so your if-statements match.

Try switching it; if A + B == C, or if A + C == B, or if B + C == A, then it's not a triangle, otherwise it is.

Campbell Ritchie
Sheriff
Posts: 48968
60
Why are you using one input and the % operator? What happens if your triangle has sides 10, 11 and 12? Why not use three inputs to the three sides?

If you are having problems with Exceptions with Scanner try entering all three numbers in one line
10 11 12
10
11
12
. . . and ask again because there is a pitfall with Scanner which you may not be aware of.

Campbell Ritchie
Sheriff
Posts: 48968
60
You are also testing wrongly. You want to test that a + b > c (don't use capitals or upper-case for variables) AND b + c > a AND a + c > b all together. Your application tests for them individually, and one of them will always turn out to be true.

Nick Hallloran
Greenhorn
Posts: 19
I did a little revising and it finally is starting to show false statements that are false and true statements that are true. Thank you for all of your responses. There is just one more thing, How do i enable the user to be able to input three numbers on the same line with spaces in between the numbers?

BTW In the question that I am doing in my textbook asks for all of the numbers on one line, not up and down or individually.

Campbell Ritchie
Sheriff
Posts: 48968
60
You get something looking like this:
Please enter the three numbers: 3 4 5
Bingo! A real triangle
If you have problems if you enter the three numbers on separate lines, ask again. And I shan't answer because I am going away for the weekend.