-1

Hi people I just need a quick fix on how to do the validation for this question. Unfortunately the book I am using doesnt seem to provide a solution after attempting the program. Here is the the question.

"Write an input validation program that asks the user to enter 'Y', 'y' 'N' or 'n' "

keep in mind it must be done only using a while loop any help would be appreciated.

while(answer != "y" || answer!= "n" )
    {
        System.out.println("Please use a  Y or N");
            answer = keyboard.nextLine();
    }

that is my current loop but it doesnt seem to work even if i enter y or n in it.

The current output i keep getting is "Please use a Y or N" regardless if I enter the correct letter

Aersor
  • 11
  • 3
  • 2
    Also consider using `equalsIgnoreCase` instead of `equals` , it will manage the case sensitivity for you. – Arnaud Oct 26 '17 at 15:12
  • 2
    Besides String comparaison, you should also replace the "or" comparaison `||` with an "and" comparaison `&&`. – AntonH Oct 26 '17 at 15:12
  • 1
    And consider changing the condition to an **&&** and not **||**, it's not doing what you're thinking – Frakcool Oct 26 '17 at 15:13
  • You guys are all wrong. He is asking about something entirely different - he wants to know how to make such loop. – Vlasec Oct 26 '17 at 15:13
  • I appreciate the feedback but yes I am asking if someone is able to help me correct my code. I cant use equalsIgnoreCase because thats not on this chapter. Its just using the while loop to do a simple validation is all. I cant use any other tools sadly. – Aersor Oct 26 '17 at 15:16
  • @Aersor: If you can't use `.equalsIgnoreCase()` (which is a silly requirement) then just use `.equals()` and include all the cases in the comparison. – David Oct 26 '17 at 15:17

1 Answers1

1

This would almost call for a do-while cycle:

do {
    answer = keyboard.nextLine();
    System.out.println("Please use a  Y or N");
} while(!answer.equals("y") && !answer.equals("n"))

Except for one problem, this shows you the warning anyway. Using while, there are two options:

answer = keyboard.nextLine();
while(!answer.equals("y") && !answer.equals("n")) {
    System.out.println("Please use a  Y or N");
    answer = keyboard.nextLine();
} 

Or, the "infinite loop with break" approach, that saves you the duplicity, but introduces a "goto":

while(true) {
    answer = keyboard.nextLine();
    if (answer.equals("y") || answer.equals("n")) {
        break;
    }
    System.out.println("Please use a  Y or N");
} 

The comparison using == is a disaster - never use it with String, always use equals. Otherwise you are comparing the reference rather than the value.

I almost forgot about the uppercase letters: you can replace equals with equalsIgnoreCase as mentioned in comments. However if it's not available to you because of the silly requirements, simply make the comparison with "Y" and "N" as well.

Vlasec
  • 5,500
  • 3
  • 27
  • 30
  • I really appreciate this answer I have taken the first segement of the code as thats all I am required to do. I will be telling my lecturer tomorrow that I had to use the !.answer.equals("Y") part instead as comparing strings using == is just messy. Thank you so much everyone for the help :) – Aersor Oct 26 '17 at 15:23
  • If your teacher really told you to use `==` to compare value of String or any object, then he should learn first before teaching. This is a big NOPE in Java. – Vlasec Oct 26 '17 at 15:27