Results 1 to 7 of 7
  1. #1
    basla is offline Member
    Join Date
    Mar 2011
    Posts
    13
    Rep Power
    0

    Default Can't figure out why my Else statement won't work

    Hello all,

    I have just started learning to program and am driving myself crazy trying to figure out why a program I wrote won't print (lines 48-50 below). It compiles and runs, it just doesn't print line 49.

    The point of the program is to determine if 3 numbers input by a user could represent the sides of a triangle. The code I came up with works if the numbers could be a triangle (it prints "The numbers you entered could represent the sides of a triangle.") but it won't print a message if it's not a triangle. I tried surrounding the IF and ELSE statements with various arrangements of brackets with no luck. I'm sure it's something obvious that I'm overlooking but i don't know what it is. Anyone see where I'm going wrong?

    Thanks!

    PS- I don't know how to include the box with the scroll bar in my post. Sorry!


    8 import java.util.Scanner;//import Scanner class so we can get user input from keyboard
    9 public class Triangle //name of program
    10 {
    11 //Main method begins execution of Java program
    12 public static void main( String[] args )
    13 {
    14 //Scanner lets us take in information typed in by keyboard
    15 Scanner keyboard = new Scanner (System.in);
    16
    17 double number1;//first number to compare
    18 double number2;//second number to compare
    19 double number3;//third number to comapre
    20
    21 //Prompt user to enter number
    22 System.out.println(" Please enter a number greater than 0.");
    23 //Read number
    24 number1 = keyboard.nextDouble();
    25
    26 //Prompt user to enter number
    27 System.out.println(" Please enter a second number greater than 0.");
    28 //Read number
    29 number2 = keyboard.nextDouble();
    30
    31 //Prompt user to enter number
    32 System.out.println(" Please enter a third number greater than 0.");
    33 //Read number
    34 number3 = keyboard.nextDouble();
    35
    36 //Compare numbers. If they meet the requirements print to screen
    37 //that it could be a triangle.
    38
    39 if ( (number1 + number2) >= number3 )
    40
    41 if ( (number2 + number3) >= number1 )
    42
    43 if ( (number3 + number1) >= number2)
    44 System.out.println( "The numbers you entered could represent the sides of a triangle.");
    45
    46 //if numbers don't satisfy above requirement print to screen
    47 //that the numbers can not represent a triangle.
    48 else
    49 System.out.println("The numbers you entered can not represent the sides of a triangle.");
    50
    51
    52 }//end main method
    53 }//end class

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,956
    Rep Power
    8

    Default

    Hint: surround your if and else blocks with {} to better see where your groupings are. Have you stepped through this with a debugger yet?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,956
    Rep Power
    8

    Default

    Also- You could use the && operator instead of nesting if statements like that.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    FlipPoker@gmail.com is offline Senior Member
    Join Date
    Mar 2011
    Posts
    103
    Rep Power
    0

    Default

    Please put your code inside a CODE block.

    It might be easier if you combine the if statements into one. The way you have it, the else statement doesn't execute if one of the first two if statements fails.
    Java Code:
    		if ( ((number1 + number2) >= number3) && ((number2 + number3) >= number1 ) && ((number3 + number1) >= number2) )
    			System.out.println( "The numbers you entered could represent the sides of a triangle.");
    		else 
    			//if numbers don't satisfy above requirement print to screen
    			//that the numbers can not represent a triangle.
    			System.out.println("The numbers you entered can not represent the sides of a triangle.");

  5. #5
    basla is offline Member
    Join Date
    Mar 2011
    Posts
    13
    Rep Power
    0

    Default

    Thanks so much Kevin and FlipPoker! I used {} on the second two if statements and it's working now. I like the idea of combining the IF statements too.

    One more question relating to this forum- how do you post a code block?

    Thanks again!

  6. #6
    FlipPoker@gmail.com is offline Senior Member
    Join Date
    Mar 2011
    Posts
    103
    Rep Power
    0

    Default

    In the editor, click the # sound on the tool bar. That will give you code tags. Just place your code in between them. Click "Preview" to make sure you did it right.

  7. #7
    basla is offline Member
    Join Date
    Mar 2011
    Posts
    13
    Rep Power
    0

Similar Threads

  1. Replies: 3
    Last Post: 02-23-2011, 07:25 PM
  2. Repaint calling Statement Doesn't Work in my code ?
    By nitin_daviet88 in forum CLDC and MIDP
    Replies: 2
    Last Post: 07-24-2010, 03:09 PM
  3. SQL statement doesn't work....
    By pbaudru in forum New To Java
    Replies: 4
    Last Post: 04-08-2010, 09:51 PM
  4. Need help - I can't figure it out.
    By Joshsmith in forum New To Java
    Replies: 2
    Last Post: 10-23-2009, 10:12 PM
  5. I can't figure this out
    By silvia in forum New To Java
    Replies: 3
    Last Post: 07-20-2007, 04:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •