When doing a string comparison between the data entered and already set data.

·

1 min read

Don’t use == as it will not work.

import java.util.Scanner;
public class Integer{
  public static void main(String a[]){
    Scanner scanner = new Scanner(System.in);
    System.out.print("Username: ");
    String name = scanner.nextLine();

    if(name == "John"){ // it will not work
      System.out.println("Correct name");
    }

    scanner.close();
  }
}

So use the .equal() command to do that comparison

import java.util.Scanner;
public class Integer{
  public static void main(String a[]){
    Scanner scanner = new Scanner(System.in);
    System.out.print("Username: ");
    String name = scanner.nextLine();

    if(name.equals("John")){
      System.out.println("Correct name");
    }

    scanner.close();
  }
}