Hello,

Are we allowed to assign different values to just one variable? For example, gender = [Female, female, fEmale] so if one a user enters any of the specified ones then he/she will be taken to the next step/level.

I'm just doing a tute at the moment and coming up with close solution to what I want to do.

public static void main (String args[]){
		String gen1 = "Male";
		String gen2 = new ArrayList;       has errors
		String gen2 =[Female, female];   has errors 
		String userInput;
		//Taking user input now.
		Scanner scan = new Scanner(System.in);
		
		System.out.println("Please enter your gender:");
		userInput = scan.nextLine();
			
		if (userInput.equals(gen1)) {
			System.out.println("Yes, true. You're a boy");
		}else{
			System.out.println("No, you Are not a boy. So, you must be a girl");
			
		}


Anyone could tell the difference between .equals() function and == function and when they are used since they're very much doing the same job that is comparing two things.

Are we allowed to assign different values to just one variable?

only arrays can contain more than one value.

equals is a method that the writer of a class can define what the method is supposed to do. For Strings it compares the contents of the two String objects.
== is a primitive operater that compares the contents of two variables like int or double. If the variables are object references, == tests if the two variables refer to the same object.
For example:
String s1 = "asdf"; // Create s1 as a reference to an object containing "asdf"
String s2 = s1; // Create s2 as a reference and give it the same value as s1
Now s1 == s2 is true. They both point to the same object: "asdf"

Edited 5 Years Ago by NormR1: n/a

If you are worried about them typing their answer in with crazy caps in the middle of the word, then use the equalsIgnoreCase();

Also, you might want to .trim() away leading and trailing spaces.

You might want to check for "male", and if they entered a non-empty string and you don't recognize it as "male" or "female", you could ask them to try again.

Use input can be complex and tedious to code. There are so many ways the user can "mess it up." ;->

I think you use == when you are dealing with primitives, or one value. You use .equals when you are dealing with multiple values, for example a String is an array of char which makes the string have multiple values in it.

I think you use == when you are dealing with primitives, or one value. You use .equals when you are dealing with multiple values,

Not a good run to follow. Not all objects have multiple values. Even those with one value should use the equals() method.

Strings are treated very specially by the compiler and JVM and can sometimes be compared with ==. But in general the method is better.

This question has already been answered. Start a new discussion instead.