Results 1 to 5 of 5

Thread: Java if statements not working correctly

  1. #1
    Join Date
    Sep 2010
    Posts
    5,762
    Mentioned
    136 Post(s)
    Quoted
    2739 Post(s)

    Default Java if statements not working correctly

    Another reason java is annoying to code in. Anyway my code looks like:

    Code:
    public class Test {
    
    	static String Description = "bad";
    
    	public static void main(String[] args) {
    		boolean Good;
    		
    		if (Description == "shit"); {
    			
    			Good = false;
    			}
                 
    		else if (Description == "great"); {
    			Good = true;
    		}
    		
    
    		if (Good == true); { 
    			System.out.println("Great!");
    			
    			System.exit(0);
    			
    		}
    			
    		else if (Good == false); {
    				System.out.println("Bad!");
    				
    				System.exit(0);
    				
    			}
    			}
    }

    And it sais ' error delete token else '

    Well, if i delete the else statements then it outputs "Great" no matter what!

  2. #2
    Join Date
    Jan 2009
    Location
    Turlock/LA, California
    Posts
    1,494
    Mentioned
    3 Post(s)
    Quoted
    66 Post(s)

    Default

    no ";" after an if

  3. #3
    Join Date
    Sep 2010
    Posts
    5,762
    Mentioned
    136 Post(s)
    Quoted
    2739 Post(s)

    Default

    Code:
    public class test {
    
    	static String Description = "bad";
    
    	public static void main(String[] args) {
    		boolean Good;
    		
    		if (Description == "shit") {
    			
    			Good = false;
    			}
                 
    		 if (Description == "great") {
    			Good = true;
    		}
    		
    		 else {
    			 Good = false;
    		 }
    
    		  if (Good == true); { 
    			System.out.println("Great!");
    			
    			System.exit(0);
    			
    		}
    			
    		 if (Good == false); {
    				System.out.println("Bad!");
    				
    				System.exit(0);
    				
    			}
    			}
    }
    Still sais good everytime.

  4. #4
    Join Date
    Jan 2009
    Location
    Turlock/LA, California
    Posts
    1,494
    Mentioned
    3 Post(s)
    Quoted
    66 Post(s)

    Default

    Code:
    public class test {
    
    	static String Description = "bad";
    	
    	public static void main(String[] args) {
    		boolean Good;
    		
    		if (Description == "shit") {
    			Good = false;
    		} else if (Description == "great") {
    			Good = true;
    		} else {
    			Good = false;
    		} 
    		if (Good) { 
    			System.out.println("Great!");
    			System.exit(0);
    		} else {
    			System.out.println("Bad!");
    			System.exit(0);
    		}
    	}
    }
    ^ that should work (dont have a java compiler on me atm :/ )

  5. #5
    Join Date
    Jan 2011
    Location
    Denver, CO
    Posts
    1,351
    Mentioned
    2 Post(s)
    Quoted
    72 Post(s)

    Default

    Don't compare strings with the equality operator, that checks if they point to the same object. use equals instead:

    java Code:
    Description.equals("shit")

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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