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 4 of 4

Thread: I can't find out what is wrong with my code. Please Help!

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I can't find out what is wrong with my code. Please Help!

    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.
    import java.util.Scanner;
     
    public class Triangle {
      public static void main(String[] args) {
    	 Scanner input = new Scanner(System.in);
     
    	 System.out.print("Enter three edges:");
    	 int Sides = input.nextInt();
     
    	 int C = Sides % 10;
    	 int RemainingNumber = Sides / 10;
    	 int B = RemainingNumber % 10;
    	 RemainingNumber = RemainingNumber / 10;
    	 int A = RemainingNumber % 10;
     
    	 if (A + B > C && A + B != C)
    		 System.out.println("Can edges" + A + "," + B + ", " + C + "form a triangle?" + "true");
     
    	 else if (A + C > C && A + C != C)
    	     System.out.println("Can edges" + A + "," + B + ", " + C + "form a triangle?" + "true");
     
    	 else if (B + C > C && B + C != C)
             System.out.println("Can edges" + A + "," + B + ", " + C + "form a triangle?" + "true");
     
        else
         System.out.println("Can edges" + A + "," + B + ", " + C + "form a triangle?" + "false");
     
     
     
       }
    }
    Last edited by helloworld922; February 10th, 2010 at 03:35 PM.


  2. #2
    Member
    Join Date
    Jan 2010
    Location
    Oxford, UK
    Posts
    30
    Thanks
    2
    Thanked 7 Times in 7 Posts

    Default Re: I can't find out what is wrong with my code. Please Help!

    You have written
    else if(A+C>C)
    when you intended
    else if(A+C>B)
    and similarly for B+C.

    Also, your code can be simply improved. The second half of each of your if statements is redundant. If, for example, A+B>C is true, then of course A+B=C is false. Finally, your program only makes sense if the user enters three integers less than 10. Why not ask for the three edge lengths separately?

    And please put code tags around your code, and indent it sensibly.

  3. #3
    Junior Member
    Join Date
    Feb 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I can't find out what is wrong with my code. Please Help!

    I added the changes you told me about. However, it still shows formulas as being true. I entered 1,4,9. Also I thought the same thing about making the user be able to enter each integer individually, but the question specifically asks for three integers on one line. Also I looked up how to put code tags on my code, but I could not seem to figure it out while I was posting.








    import java.util.Scanner;
     
    public class Triangle {
      public static void main(String[] args) {
    	 Scanner input = new Scanner(System.in);
     
    	 System.out.print("Enter three edges:");
    	 int Sides = input.nextInt();
     
    	 int C = Sides % 10;
    	 int RemainingNumber = Sides / 10;
    	 int B = RemainingNumber % 10;
    	 RemainingNumber = RemainingNumber / 10;
    	 int A = RemainingNumber % 10;
     
    	 if (A + B > C)
    		 System.out.println("Can edges" + A + ", " + B + " , " + C + "form a triangle?" + "true");
     
    	 else if (A + C > B)
    	     System.out.println("Can edges" + A + "," + B + " , " + C + "form a triangle?" + "true");
     
    	 else if (B + C > A)
             System.out.println("Can edges" + A + " , " + B + " , " + C + "form a triangle?" + "true");
     
     
     
         else
         System.out.println("Can edges" + A + " , " + B + " , " + C + "form a triangle?" + "false");
     
     
     
       }
    }
    Last edited by Freaky Chris; February 10th, 2010 at 10:59 AM.

  4. #4
    Member
    Join Date
    Jan 2010
    Location
    Oxford, UK
    Posts
    30
    Thanks
    2
    Thanked 7 Times in 7 Posts

    Default Re: I can't find out what is wrong with my code. Please Help!

    Oh wait, now I see why what you're doing is wrong. For A, B, C to form a triangle, it has to be true that A+B>C, A+C>B and B+C>A. In other words, all three have to be true. The reason your program was never falling through to the final else statement is that at least one of those inequalities will always be true.

    As for the other point, even if all the input is supposed to be on one line, the user should be able to enter any three positive integers, separated by (say) spaces.

Similar Threads

  1. i'm new to java. whats is wrong in this code?
    By igorek83 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 30th, 2009, 08:38 PM
  2. Where am I going wrong? :)
    By KevinGreen in forum What's Wrong With My Code?
    Replies: 9
    Last Post: October 18th, 2009, 12:03 AM
  3. help whats wrong
    By silverspoon34 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 3rd, 2009, 01:41 AM
  4. [SOLVED] whats wrong with my IDE
    By chronoz13 in forum Java IDEs
    Replies: 2
    Last Post: August 27th, 2009, 06:34 AM
  5. Generation of Palindrome number in Java
    By tina.goyal in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 26th, 2009, 08:49 AM