Help with String variables

Hello, I’m a high-school student taking beginning Java, and I’m currently working on my final project (a simple RPG). It’s going pretty well, except for one part: is there a way to check if a String variable does not equal something? I know this example won’t work, because you can’t use == with Strings to compare two variables (usually), but is there another way to do this? Any suggestions would be greatly appreciated, thanks.

public static void genderE(String gender, int picNum)
{

	while ([b]gender != "male" || gender != "female")[/b]
		{
			System.out.println("You have entered an invalid gender. Please try again:");
			gender = In.getString();
		}
	
	if (gender.equalsIgnoreCase("male"))
		picNum += 0;
	else if (gender.equalsIgnoreCase("female"))
		picNum += 100;
	else 
		System.out.println("You have entered an invalid gender. Please try again:");
}

Not?

 ! 

It still doesn’t work, because I get errors just for that, no matter what I enter, even if it’s right.

You don’t get errors for stuff that is right. If you told us what you tried and what the error message is we might be able to help you.


if( !"male".equals("female") ) {
    System.out.println("Always true!");
}

Sorry, I didn’t understand what you meant with the single ! (extremely newbie-ish, and forgetful). That works, thanks.

The ! is a negation of a boolean expression. For example

boolean male;

if(!male){
System.out.println("it’s a woman);
}else{
System.out.println("it’s a man);
}

… and so mystring.equals(“Something”) returns boolean and you can negate it with prefix ! .
!mystring.equals(“Something”) — that means mystring is NOT equals to string “Something”, as guys already said.