Results 1 to 9 of 9
  1. #1
    jgarner is offline Member
    Join Date
    Jul 2015
    Posts
    11
    Rep Power
    0

    Default If else problems!

    Hello everyone! I'm new to Java and am trying to practice an if/else statement with different operators. I am getting a list of 22 errors in the code, which leads me to believe I've completely messed up! Can you please help lead me in the right direction?

    Java Code:
    //demonstrate the relational and logical operators.
    class RelLogOps {
    public static void main(String augs[]) {
    
    int i = 10;
    int j = 11;
    boolean  = b1;
    boolean = b2;
    
    	if(i < j) { System.out.println("i is less than j");	 }
    	 else { System.out.println("i isn't less than j"); 	  }
    	if(i <=j) { System.out.println("i less than or equal to j");	 }
    	 else { System.out.println("i is not less than or equal to j");	  }
    	if(i != j) { System.out.println("i not equal to j");	 } 
    	 else { System.out.println("i is equal to j");	  	 }
    	if(i == j) { System.out.println("i is equal to j");    	}
    	 else { System.out.println("is is not equal to j");      }
    	if(i >= j) { System.out.println("i is greater than or equal to j"); } 
    	  else { System.out.println("i is not greater than or equal to j");  }
    	if(i > j) { System.out.println("i is greater than j");	}
    	  else { System.out.println("i is not greater than j");	  }
    
    b1 = true;
    b2 = false;
    
    	if(b1 & b2) { System.out.println("b1 and b2 are both the same");}
             else System.out.println("b1 and b2 aren't the same");   }
    	if(!(b1 and b2)) { System.out.println("b1 and b2 aren't the same");} 
             else System.out.println("b1 and b2 are the same");  }
    	if(b1 | b2) { System.out.println("b1 | b2 is true");} 
    	 else { System.out.println("b1 | b2 isn't true"); }
    	if(b1 ^ b2) { System.out.println("b1 and b2 are different"); }
    	 else { System.out.println("b1 and b2 are the same.");    }
    
    	}
    }



    Basically I'm just comparing a couple of values I preselected and am letting it run through each operator and print the result. From what my book reads, the if/else format should be

    Java Code:
    ...
    .....
       if(value operator value) {  statement; 
                 }
    
       else {    statement; 
              }
    
    ....
    
    .....
    .....


    Am I not allowed to do this comparison more than once in a block? Thanks for any help I receive! It's been really exciting learning Java so far! I'm only on day 2, but it feels so rewarding making my own programs and using my shell.
    Last edited by jgarner; 07-29-2015 at 05:25 AM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: If else problems!

    First, it should be

    Java Code:
    boolean b1;
    boolean b2;
    Your braces are messed up. I recommend you go back and reformat your code like this.

    Java Code:
    if (A && B) {
         // do something
    } else {
        // do something else
    }  
    
    // or
    
    if (A && B) {
      // do something
    } else if (B && C) {
       // do something
    }  else {
       // do this instead
    }
    And you have an "and" in an if statement. Use either && or & (read up on the difference).

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    jgarner is offline Member
    Join Date
    Jul 2015
    Posts
    11
    Rep Power
    0

    Default Re: If else problems!

    Quote Originally Posted by jim829 View Post
    First, it should be

    Java Code:
    boolean b1;
    boolean b2;
    Your braces are messed up. I recommend you go back and reformat your code like this.

    Java Code:
    if (A && B) {
         // do something
    } else {
        // do something else
    }  
    
    // or
    
    if (A && B) {
      // do something
    } else if (B && C) {
       // do something
    }  else {
       // do this instead
    }
    And you have an "and" in an if statement. Use either && or & (read up on the difference).

    Regards,
    Jim


    Jim, thanks for your help! After some adjustments, I got it to work! I shall post my corrected code below in case someone would like to see what I did. Maybe I can get some more veteran suggestions to clean it up further!


    1) I had forgotten that the boolean type didn't require an = in the statement. I'm going to have to look into why that is the case so I'll drill it in my head!

    2) I altered my braces to the format you suggested. It really feels better your way. My book (Oracle: A Beginners Guide to Java) hasn't really shown me any if/else statements as long as mine. I had taken their code and added the else statement for more practice. They haven't given me much work with if/else yet and I wanted to practice it. I haven't gone through my old code to see where I messed up brackets, but I will go back and find which ones I missed.

    3) You were correct. It took me a minute to find it. That was just a brain fart. I was thinking and and just typed it out instead of using the operator.


    Here is the working code!
    Java Code:
    //demonstrate the relational and logical operators.
    class RelLogOps {
    public static void main(String augs[]) {
    
    int i = 10;
    int j = 11;
    boolean b1;
    boolean b2;
    
    	if(i < j) { 
    	System.out.println("i is less than j");
    	}  else { System.out.println("i isn't less than j");
    	   }
    
    	if(i <=j) { 
    	System.out.println("i less than or equal to j");
    	} else { System.out.println("i is not less than or equal to j");
    		}
    
    	if(i != j) { 
    	System.out.println("i not equal to j");
    	} else { System.out.println("i is equal to j");
    	       }
    
    	if(i == j) { 
    	System.out.println("i is equal to j");
    	 } else { System.out.println("is is not equal to j");
    	      }
    
    	if(i >= j) { 
    	System.out.println("i is greater than or equal to j"); 
    	 } else { System.out.println("i is not greater than or equal to j");
    		}
    
    	if(i > j) { 
    	System.out.println("i is greater than j");
    	}  else { System.out.println("i is not greater than j");
    		}
    
    b1 = true;
    b2 = false;
    
    	if(b1 & b2) { System.out.println("b1 and b2 are both the same");
    	} else { System.out.println("b1 and b2 aren't the same");
    	       }
    
    	if(!(b1 & b2)) { System.out.println("b1 and b2 aren't the same");
    	 } else { System.out.println("b1 and b2 are the same");
    	        }		
    
    	if(b1 | b2) { System.out.println("b1 | b2 is true"); 
    	 } else { System.out.println("b1 | b2 isn't true"); 
    	      }
    
    	if(b1 ^ b2) { System.out.println("b1 and b2 are different");
    	}  else { System.out.println("b1 and b2 are the same.");
    	      }
    
    	}
    }
    If anyone has any suggestions on how to make my code cleaner or more efficient I would really value your comments!

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: If else problems!

    You really should format it the way jim suggested.
    As it stands it's still hard to spot the structure of the code.

    Your boolean checks in the second half ought to be && and ||.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: If else problems!

    Quote Originally Posted by jgarner View Post
    If anyone has any suggestions on how to make my code cleaner or more efficient I would really value your comments!
    That is not really a worthy thing at this point, the code is very unrealistic. I'd move on to the next thing and start worrying about clean code when you start to write something that is "real" and not just some test code.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: If else problems!

    Enter keys are free, spaces are too! Use them. Make your code readable. The way it's formatted now, you're just asking for trouble. The compiler will catch the unbalanced { and }, but there are some cases where it won't:

    Java Code:
    if ( a == b) {}
    System.out.println(a);
    System.out.println(b);
    Or this:
    Java Code:
    if ( a == b)
    System.out.println(a);
    System.out.println(b);
    Then there is Tolls's comment: Your boolean checks in the second half ought to be && and ||.


    Reformatted. I know this is twice as long, but it reads twice as fast, since my eyes don't have to parse all the misaligned { and } and make my brain bleed.
    Java Code:
    class RelLogOps {
        public static void main(String augs[]) {
            int i = 10;
            int j = 11;
            boolean b1;
            boolean b2;
    
            if (i < j) {
                System.out.println("i is less than j");
            }
            else {
                System.out.println("i isn't less than j");
            }
    
            if (i <= j) {
                System.out.println("i less than or equal to j");
            }
            else {
                System.out.println("i is not less than or equal to j");
            }
    
            if (i != j) {
                System.out.println("i not equal to j");
            }
            else {
                System.out.println("i is equal to j");
            }
    
            if (i == j) {
                System.out.println("i is equal to j");
            }
            else {
                System.out.println("is is not equal to j");
            }
    
            if (i >= j) {
                System.out.println("i is greater than or equal to j");
            }
            else {
                System.out.println("i is not greater than or equal to j");
            }
    
            if (i > j) {
                System.out.println("i is greater than j");
            }
            else {
                System.out.println("i is not greater than j");
            }
    
            b1 = true;
            b2 = false;
    
            if (b1 & b2) {
                System.out.println("b1 and b2 are both the same");
            }
            else {
                System.out.println("b1 and b2 aren't the same");
            }
    
            if (!(b1 & b2)) {
                System.out.println("b1 and b2 aren't the same");
            }
            else {
                System.out.println("b1 and b2 are the same");
            }
    
            if (b1 | b2) {
                System.out.println("b1 | b2 is true");
            }
            else {
                System.out.println("b1 | b2 isn't true");
            }
    
            if (b1 ^ b2) {
                System.out.println("b1 and b2 are different");
            }
            else {
                System.out.println("b1 and b2 are the same.");
            }
    
        }
    }
    ]
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: If else problems!

    Quote Originally Posted by SurfMan View Post
    Enter keys are free, spaces are too!
    There is more cost than only money. They're covered in germs!
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  8. #8
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: If else problems!

    Quote Originally Posted by gimbal2 View Post
    There is more cost than only money. They're covered in germs!
    And then you get sick, and then you can't work, and then your boss can't make money, and then then the company goes broke, and then you have no money either, and then you live on the streets, and then you drink from the same bottle as the other homeless guy, and then you die of dysentery. I see where you're going! Good thinking :)
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  9. #9
    jgarner is offline Member
    Join Date
    Jul 2015
    Posts
    11
    Rep Power
    0

    Default Re: If else problems!

    Thanks for your formatting tips! My original code followed how the book taught it, but I found it incredibly difficult to read. My reformatted code is much easier on the eyes. I'll probably start inserting my "else" statement on the line following the } from the "if" statement.

    Again, thanks for the help! I realise that this code is something I'll probably never do in real life. Just did it to get proper syntax drilled in my head.

Similar Threads

  1. Two problems
    By frobiz176 in forum New To Java
    Replies: 2
    Last Post: 09-25-2013, 06:16 AM
  2. GUI problems on OSX
    By Melodia in forum New To Java
    Replies: 15
    Last Post: 09-12-2013, 07:33 PM
  3. InputStream/Jar Problems/File IO Problems
    By rdjava in forum Advanced Java
    Replies: 31
    Last Post: 01-17-2011, 11:12 AM
  4. RMI Problems
    By michaeln31 in forum New To Java
    Replies: 4
    Last Post: 10-23-2010, 12:52 PM
  5. JAR problems
    By Evil Smurf in forum New To Java
    Replies: 4
    Last Post: 09-01-2009, 01:17 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
  •