0

How to validate input beetween 2 String described as Y and N using scanner ?

System.out.println("Are you sure?");
String StrInput = scan.nextLine();

if(strInput = Y && StrInput = N){
    System.out.println("Nice choice");
}else{
    System.out.println("Please Input only with Y or N");
}
3
  • i know it didnt work, but i want to know how the right logic for that, i have no clue, please help, thank you! Commented Sep 14, 2020 at 15:38
  • 1
    Use equals() method of String class to check equality of two string like strInput.equals("Y") Commented Sep 14, 2020 at 15:41
  • Thank you! it works Commented Sep 14, 2020 at 15:57

1 Answer 1

1

As said by Rono as well Use Equals and use || condition between input checking, it can be either Y or N. Below snippet would help

    System.out.println("Are you sure?");
    String strInput = scan.nextLine();

    if(strInput.equals("Y") || strInput.equals("N")){
        System.out.println("Nice choice");
    }else{
        System.out.println("Please Input only with Y or N");
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.