This week's giveaway is in the Java/Jakarta EE forum.We're giving away four copies of Java EE 8 High Performance and have Romain Manni-Bucau on-line!See welcome thread
Win a copy of Java EE 8 High Performance this week in the Java/Jakarta EE forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# if statement problem

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.

Sheriff
Posts: 21209
87
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.

Marshal
Posts: 57499
175
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
Marshal
Posts: 57499
175
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
Marshal
Posts: 57499
175
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.

 You've gotta fight it! Don't give in! Read this tiny ad: ScroogeXHTML 7.2 - RTF to HTML5 / XHTML converter https://coderanch.com/t/692027/ScroogeXHTML-RTF-HTML-XHTML-converter