Java Data Type How to - Read and check strings from user input in console








Question

We would like to know how to read and check strings from user input in console.

Answer

/*from w w w .  j a  va2  s  . c  o  m*/
import java.util.Scanner;

public class Main {
  public static void main(String[] args) {

    Scanner input = new Scanner(System.in);
    String answer = input.nextLine();

    if ("yes".equals(answer)) {
      System.out.println("Yea!");
    } else {
      System.out.println("No :(");
    }
  }
}

The code above generates the following result.