Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: string comparison java

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Thanked 0 Times in 0 Posts

    Default string comparison java


    Could anyone tell me why the code below is not working?

    	 if (!gender.equals("man") || !gender.equals("female"))
    	 System.out.println("unrecognized gender!." + " Please try again");
    	 return amount = 50;

    Whatever I type in gender (either man, female or sth else) it displays

    unrecognized gender!." + " Please try again and amount is 50. In other words
     if (!gender.equals("man") || !gender.equals("female"))
    is not working. Can anyone explain me why?


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Thanked 329 Times in 305 Posts

    Default Re: string comparison java

    So your if statement says: if gender is not "man" OR if gender is not "female"
    Consider that an OR statement checks BOTH conditions independently. So if you enter "female", the first condition of your OR statement is true, since "female" is not "man". If you enter "man", the first condition is false but the second condition is true, since "man" is not "female".
    What you need to do is check: if gender is not "man" AND if gender is not "female" (or in other words, using && instead of ||)

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Member GoodbyeWorld's Avatar
    Join Date
    Jul 2012
    Hidden command post deep within the bowels of a hidden bunker somewhere under a nondescrip building
    My Mood
    Thanked 25 Times in 25 Posts

    Default Re: string comparison java

    Although java does allow

    return amount = 50;

    It is an assignment statement and it assigns to the variable amount the value of 50 and then returns the variable amount. Also, from your code, it seems that you're returning the same thing regardless of whether it goes into the if or the else.

    Did you mean to do that?

    (Note, I actually had been unsure if java even allowed such a statement, I knew that C++ did allow something like (while amount = 50) whereas java did not and assumed java wouldn't allow a return that did an assignment statement either, but it apparently does allow it here.)

Similar Threads

  1. Replies: 3
    Last Post: April 25th, 2013, 04:21 PM
  2. Java int comparison
    By maple1100 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: March 3rd, 2013, 07:45 PM
  3. table comparison
    By awecode in forum JDBC & Databases
    Replies: 2
    Last Post: October 12th, 2010, 09:37 AM
  4. Need help with array comparison
    By raidcomputer in forum Collections and Generics
    Replies: 4
    Last Post: November 17th, 2009, 01:55 PM
  5. Replies: 2
    Last Post: November 3rd, 2009, 06:28 AM