chriscoyle
chriscoyle

Reputation: 1

Comparing User Input From The Keyboard To A String Using A While Loop

I"m having problems trying to get my program to detect when the user enters Y for Yes and N for No.

This Is the code...

import java.util.Scanner;

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

        System.out.println("How Many Marks Was Your Practicals Out Of?");
        Scanner scan = new Scanner (System.in);
        int practicalsOutOf = scan.nextInt();

        System.out.println("Okay And What Did You Get Out Of:" + "  " + practicalsOutOf);
        Scanner scan2 = new Scanner (System.in);
        int whatYouGotInPracticals = scan.nextInt();

        System.out.println("Okay so you got" + "  " + whatYouGotInPracticals + "/" + practicalsOutOf + "  " + "For Your Practicals Correct?");
        scan.nextLine();


        String answer = scan.nextLine();
        while ( !"Y".equals(answer) || !"N".equals(answer)){
            System.out.println("Please Type A Capital Y If Correct Or A Capital N If Incorrect");
            scan.nextLine();
        }
        System.out.println("Thank You");


    }
}

When I type Y or N it's not recognising it as a correct answer the program continues to run through the look displaying:

Please enter a Capital Y if correct or a capital N if Incorrect

So it's not exiting the loop?

If I get rid of the OR comparison and just keep it so the program is only looking for the Y I can get the program to exit the loop if I type in the correct answer first time otherwise it ignores a correct answer and continues to run through the loop.

When I put the OR comparison back in so that the program can accept Y or N it won't accept any of them and will continue to run through the loop, even if I type in the correct answer first time.

I really can't see what I'm doing wrong here. Any help would be great!

Thanks

Upvotes: 0

Views: 2405

Answers (3)

Scis
Scis

Reputation: 2984

Your while condition is not good:

!"Y".equals(answer) || !"N".equals(answer)

Means that:

If answer is not "Y" or answer is not "N" you'll continue.

As answer can never be both (the only way to satisfy this condition) this will always be true.

Just change the || to &&.

And yes, there's no need to create another identical scanner.

EDIT: Why && works:

!"Y".equals(answer) && !"N".equals(answer)

Means that if answer is not "Y" and answer is not "N" meaning the user gave us some string that is neither "Y" nor "N", which is a wrong input in our case we should ask him to try again.

Note that this condition is false if and only if answer is "Y" or "N", which is exactly what we want.

EDIT:

The other problem is due to the fact that you never assign answer in the loop(so it will have the initial value forever muhahaha :) ), please see the DEMO below..

This: scan.nextLine(); just scans and ignores the return value this : answer = scan.nextLine(); doesn't.

DEMO. DEMO2

Upvotes: 1

Kristijan
Kristijan

Reputation: 5885

You don't set answer inside the loop, you only set it once prior to the loop (thats why it works only the first time):

change

scan.nextLine();

to

answer = scan.nextLine();

Upvotes: 0

Wundwin Born
Wundwin Born

Reputation: 3475

Try with;

String answer = scan.nextLine();
while ( !"Y".equals(answer) && !"N".equals(answer)){
    System.out.println("Please Type A Capital Y If Correct Or A Capital N If Incorrect");
    answer = scan.nextLine(); //here set the next scan to answer
}

Upvotes: 0

Related Questions