Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By Thammer

Thread: If-else. For String variables.

  1. #1
    Thammer is offline Member
    Join Date
    Sep 2014
    Posts
    47
    Rep Power
    0

    Default If-else. For String variables.

    Hey guys,

    Is it possible to use a if-else statement for string variables?
    I am trying to make a program that execute different depending on the user typing in "male" or not when he/she gets asked about gender. The program execute the else part no matter what the user types in.

    Here is my code, I appreciate all the help.

    import java.util.Scanner;
    public class c4Testname {
    public static void main(String[]args){
    Scanner input = new Scanner(System.in);
    System.out.println("Type in your gender");
    String name = input.next();
    String name2 = "male";



    if (name == name2){
    System.out.print("your gender is male");
    }else{
    System.out.print("your gender is not male");
    }
    }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: If-else. For String variables.

    Use the equals() method to compare objects like Strings, not the == operator.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Menre is offline Member
    Join Date
    Feb 2008
    Posts
    79
    Rep Power
    0

    Default Re: If-else. For String variables.

    Norm, I personally do not quite understand how the equals() method would be applied here. Could you say more on that, please?

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

    Default Re: If-else. For String variables.

    Quote Originally Posted by Menre View Post
    Norm, I personally do not quite understand how the equals() method would be applied here. Could you say more on that, please?
    Code snippet from above:

    Java Code:
         if (name == name2){
                System.out.print("your gender is male");
            }else{
                System.out.print("your gender is not male");
            }
        }
    name2 was initialized to "male"
    So use name.equals(name2)

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

  5. #5
    Thammer is offline Member
    Join Date
    Sep 2014
    Posts
    47
    Rep Power
    0

    Default Re: If-else. For String variables.

    Great help, and I read some more around it and now know how to sort strings alphabetical as well :)
    DarrylBurke likes this.

Similar Threads

  1. error with sub string of tow variables
    By M ALVI in forum New To Java
    Replies: 7
    Last Post: 09-03-2013, 11:44 AM
  2. Splitting String and assigning tokens to variables
    By igorland in forum New To Java
    Replies: 3
    Last Post: 02-06-2013, 02:47 AM
  3. String named variables
    By sgthale in forum New To Java
    Replies: 3
    Last Post: 05-06-2011, 04:06 AM
  4. parse string into 2 double variables
    By woodsie2523 in forum New To Java
    Replies: 3
    Last Post: 10-26-2010, 01:46 PM
  5. Displaying variables in a string?
    By shroomiin in forum New To Java
    Replies: 4
    Last Post: 09-21-2009, 03:10 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
  •