Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By pbrockway2

Thread: Why isn't this if statement working?

  1. #1
    ocomobock is offline Member
    Join Date
    Dec 2011
    Posts
    13
    Rep Power
    0

    Default Why isn't this if statement working?

    I tried experimenting with scanners and if statements, and it didn't work. I can't really figure out why. (very new to java, don't judge me)

    Java Code:
    import java.util.Scanner;
    
    class NewEmpty5{
        public static void main(String[] args){
            
            Scanner scanner = new Scanner(System.in);
            
            System.out.println("What's your name?");
          
            String name = scanner.next();
            
            if(name == "Not Available"){
                System.out.println("I'm sorry, we aren't allowing anybody to use the name \"Not Available\"");
            }else if(name != "Not Available"){
                System.out.println("Thank you for not choosing the name \"Not Available\"");
            }
        }
    }
    Any ideas why it isn't working? I assume it's pretty obvious, so it's gonna bother me when I find out. <_<

    Output when I typed "Not Available":

    run:
    What's your name?
    Not Available
    Thank you for not choosing the name "Not Available"
    BUILD SUCCESSFUL (total time: 4 seconds)



    Thanks in advance.
    Last edited by ocomobock; 03-27-2012 at 01:13 AM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Why isn't this if statement working?

    Don't use == and != when comparing things other than primitive values (numbers etc). Instead use the the equals() method. Every class defines equals() in some way that makes sense for it: in the case of strings they are equal when they are made up of the same characters in the same order.

    Java Code:
    if(name.equals("Not Available")) {
    name.equals("whatever") is a boolean expression: that is it will be a boolean value (true or false). I mention this because you may want to use the ! operator in conjunction with such an expression to express not being equal to.
    ankiit likes this.

  3. #3
    ocomobock is offline Member
    Join Date
    Dec 2011
    Posts
    13
    Rep Power
    0

    Default Re: Why isn't this if statement working?

    Quote Originally Posted by pbrockway2 View Post
    Don't use == and != when comparing things other than primitive values (numbers etc). Instead use the the equals() method. Every class defines equals() in some way that makes sense for it: in the case of strings they are equal when they are made up of the same characters in the same order.

    Java Code:
    if(name.equals("Not Available")) {
    name.equals("whatever") is a boolean expression: that is it will be a boolean value (true or false). I mention this because you may want to use the ! operator in conjunction with such an expression to express not being equal to.
    Thank you! I know how to use the equals() method but some reason it just didn't cross my mind.

    EDIT: I edited the code and now I have another problem. How do I make the program remember that you can't use that name?

    Java Code:
    import java.util.Scanner;
    
    class NewEmpty5{
        public static void main(String[] args){
            
            Scanner scanner = new Scanner(System.in);
            
            System.out.println("What's your name?");
          
            String name = scanner.next();
            
            if(name.equals("NotTelling")){
                System.out.println("I'm sorry, we aren't allowing anybody to use the name \"NotTelling\"");
                System.out.println("What's your name?");
          
                scanner.next();
            }
            System.out.println("Hello, " + name);
        }
    }
    Output:

    run:
    What's your name?
    NotTelling
    I'm sorry, we aren't allowing anybody to use the name "NotTelling"
    What's your name?
    NotTelling
    Hello, NotTelling
    BUILD SUCCESSFUL (total time: 7 seconds)
    Last edited by ocomobock; 03-27-2012 at 03:03 AM.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Why isn't this if statement working?

    Put the code in a loop. Something along the lines of DO(get name. IF appropriate print message) WHILE(name is NotTelling)

Similar Threads

  1. 'if' statement not working?
    By Etimer in forum New To Java
    Replies: 4
    Last Post: 03-10-2012, 06:18 AM
  2. If statement is not working properly
    By Alpa in forum New To Java
    Replies: 3
    Last Post: 02-04-2012, 09:40 PM
  3. SQL statement working in one class but not another
    By Dcalladi in forum New To Java
    Replies: 4
    Last Post: 11-11-2011, 02:35 AM
  4. if statement with strings not working... again
    By hardcorebadger in forum New To Java
    Replies: 4
    Last Post: 01-11-2011, 07:02 AM
  5. Update statement not working.
    By OMFGITSROHIT in forum JDBC
    Replies: 5
    Last Post: 04-08-2010, 02:03 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
  •