I just started learning code (Java specifically), and i’m testing out a password system where when you type the password it becomes the variable “password” and it checks to see if it equals password2, the actual password. Here’s the code:
import java.util.Scanner;
public class LogicalOperators {
public static void main(String args[]){
Scanner test = new Scanner(System.in);
int age;
int password;
String password2;
password2 = "Call of Duty";
System.out.println("Please enter your age:");
age = test.nextInt();
if (age >=18) {
System.out.println("You are old enough.");
System.out.println("Please enter the password:");
password = test.nextInt();
if (password == password2) {
System.out.println("Welcome back!");
}else{
System.out.println("The password you typed was incorrect.");
}
}else{
System.out.println("You are too young.");
}
}
}
I’m trying to check in the nested if statement whether the password I typed in matched password2, “Call of Duty”; but the problem is that it doesn’t work with strings. The title of this question is the error that comes up. Can someone please help me?
when comparing strings you should use equals instead of ==
So use