Results 1 to 5 of 5
  1. #1
    porchrat is offline Senior Member
    Join Date
    Mar 2009
    Posts
    105
    Rep Power
    0

    Default Help with if-else statements

    Hi all. This is my first post on this forum and I am pretty new to Java having just started messing with it a month or so ago.

    I'm having some trouble with if-else statements and hopefully someone can help.

    It happens often and I belive I may be doing something wrong here, I have provided an example program I've been playing with:

    Java Code:
    package awesome;
    
    public class Test {
    	public static void main (String[] args)
    	throws Exception
    	{
    		String stdntNmbr;
    		double tutMark;
    		double testMark;
    		char grade;
    		
    
    		System.out.print("Please enter your student number:");
    		stdntNmbr = Input.getString();
    		
    
    		System.out.print("Please enter your average mark for tutorials:");
    		tutMark = Input.getDouble();
    		
    		
    		System.out.print("Please enter you average mark for tests:");
    		testMark = Input.getDouble();
    		
    		
    		if (((tutMark + testMark)/2) < 40);
    			grade = 'F';	
    		[B][I][U]else[/U][/I][/B]
    		{
    			double tutTestMark;
    			double exam;
    			double total;
    			
    			
    				System.out.print("Please enter your final examination mark:");
    				exam = Input.getDouble();
    				total = (((( tutMark + testMark)/2) + exam )/2 );
    				if (total >= 80)
    				{
    					grade = 'A';
    				}
    				else if (total >= 70)
    				{
    					grade = 'B';
    				}
    				else if (total >= 60)
    				{
    					grade = 'C';
    				}
    				else if (total >= 50)
    				{
    					grade = 'D';
    				}
    				else if (total >= 40)
    				{
    					grade = 'E';
    				}
    				else
    				{
    					grade = '0';
    				}
    			}
    		if ( grade == 0);
    			System.out.println("Error in grade calculation");
    		[B][I][U]else[/U][/I][/B]
    			System.out.println("Student " + stdntNmbr + " your grade is " + grade);
    	}
    }
    The two bolded, italics and underlined 'else' statments are the statements that are causing problems, eclipse tells me that it can't offer suggestions on how to fix it and that I should delete the token. I can't figure out what I'm doing wrong! It throws errors and makes the code uncompilable.

    If someone could offer an explanation that would be most helpful as I feel that I don't want to move on to more complex things without having resolved this issue first, as simple if-else statements are something I will need later.

    Thanks for your time everyone.

  2. #2
    paul pasciak is offline Senior Member
    Join Date
    Jul 2008
    Posts
    125
    Rep Power
    0

    Default You are over-using the semicolon.

    I didn't look too closely at your code.
    A common problem is that people leave out
    a semi-colon, or a brace.

    You are over-using the semi-colon.
    In your if statement, you end it too soon.

    Example:
    Java Code:
    		if (((tutMark + testMark)/2) < 40);
    			grade = 'F';
    If this is ONE conditional test, it should have
    only one semi-colon at the end. You may
    have added one in the middle, assuming that's
    where the if statement ends.

    Now the compiler thinks if the test is
    true, do nothing and go on to assign
    grade = 'F'.
    If the condition of the test is false,
    skip over the do nothing part and assign
    grade = 'F'.
    Last edited by paul pasciak; 03-22-2009 at 11:12 PM.

  3. #3
    porchrat is offline Senior Member
    Join Date
    Mar 2009
    Posts
    105
    Rep Power
    0

    Default

    Quote Originally Posted by paul pasciak View Post
    I didn't look too closely at your code.
    A common problem is that people leave out
    a semi-colon, or a brace.

    You are over-using the semi-colon.
    In your if statement, you end it too soon.

    Example:
    Java Code:
    		if (((tutMark + testMark)/2) < 40);
    			grade = 'F';
    If this is ONE conditional test, it should have
    only one semi-colon at the end. You may
    have added one in the middle, assuming that's
    where the if statement ends.

    Now the compiler thinks if the test is
    true, do nothing and go on to assign
    grade = 'F'.
    If the condition of the test is false,
    skip over the do nothing part and assign
    grade = 'F'.
    Thank you that makes sense I can't believe I couldn't see that!

    I will make the changes this evening and post the results.

    Thanks again! :)

  4. #4
    xbox_nutter is offline Member
    Join Date
    Mar 2009
    Posts
    15
    Rep Power
    0

    Default

    also tell me if I am wring but should this not also be

    Java Code:
    if (((tutMark + testMark)/2) < 40)[B]{[/B]
        grade = 'F';
    [B]}
    else[/B]{

  5. #5
    porchrat is offline Senior Member
    Join Date
    Mar 2009
    Posts
    105
    Rep Power
    0

    Default

    Thank you removing the semicolons was what it needed, can't believe that was all it was. Feel like a bit of a muppet now. Oh well I suppose it is all part of learning.

    Thank you again for your help

    Quote Originally Posted by xbox_nutter View Post
    also tell me if I am wring but should this not also be

    Java Code:
    if (((tutMark + testMark)/2) < 40)[B]{[/B]
        grade = 'F';
    [B]}
    else[/B]{
    Doesn't have to be if you're only using one line, but you're right it is proper practice to put the braces in. The compiler accepts it anyway and the logic still works as though the braces were there, you HAVE to use the braces if the 'if' contains more than one statement though.
    Last edited by porchrat; 03-23-2009 at 05:26 PM.

Similar Threads

  1. How to speed sql Statements?
    By bezudar in forum Advanced Java
    Replies: 3
    Last Post: 11-20-2008, 10:53 AM
  2. if else statements and using images
    By Joshr in forum New To Java
    Replies: 8
    Last Post: 10-05-2008, 07:51 PM
  3. Help with if else statements
    By zoe in forum New To Java
    Replies: 1
    Last Post: 07-24-2007, 08:56 PM

Posting Permissions

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