0

I am trying to get this to ask the question over and over again while the user inputs a 'Y'. and stop and return the Event.displayFinalResults(); when the user inputs a 'N'

i am getting a continuous loop right now. I am missing something or my layout it wrong. Any help would be great.

            System.out.println("Is there anymore amounts you want to add?(Y/N): ");
            Scanner keyboard = new Scanner(System.in);
            char choice = keyboard.next().charAt(0);
            choice = Character.toUpperCase(choice);
            if(choice == 'Y')
            {
                do
                {
                    System.out.println("Is there anymore amounts you want to add?(Y/N): ");
                    choice = keyboard.next().charAt(0);
                    choice = Character.toUpperCase(choice);

                    readValidateAmountType();
                    readValidateAmount(); 
                }
                while(choice =='Y');

            }
            else
            {
                Event.displayFinalResults();
            }

thanks again.

6
  • Just to be clear, the problem is that after you type in Y first, then type in N next, it stays in the while loop? Commented Feb 22, 2016 at 4:21
  • 1
    It's comparing characters, not strings, so I don't think that's the issue. Commented Feb 22, 2016 at 4:22
  • i am new. so i know i have a continuous loop because of a tiny problem. I have been working on this for hours trying different methods and now i am at a roadblock. Commented Feb 22, 2016 at 4:24
  • Yes @Michael that is correct. if you answer N first, then it works fine, but if you answer Y then N, then your in the loop Commented Feb 22, 2016 at 4:28
  • @Luke8h, Why are you doing duplicate code for input? for example System.out.println("Is there anymore amounts you want to add?(Y/N): "); writing twice! Commented Feb 22, 2016 at 4:43

5 Answers 5

3

You program asks a Yes/No question, then if you answer Yes, it enter a loop which starts by asking the same question again, before asking for the amount value.

You might want to ask for the amount value before asking the Yes/No question again.

If user answer No, the loop will exit (after asking for one more amount value), but Event.displayFinalResults() will not be executed. Drop the else clause, so Event.displayFinalResults() whether it entered the if statement or not.

System.out.println("Is there anymore amounts you want to add?(Y/N): ");
Scanner keyboard = new Scanner(System.in);
char choice = keyboard.next().charAt(0);
choice = Character.toUpperCase(choice);
if (choice == 'Y')
{
    do
    {
        readValidateAmountType();
        readValidateAmount(); 

        System.out.println("Is there anymore amounts you want to add?(Y/N): ");
        choice = keyboard.next().charAt(0);
        choice = Character.toUpperCase(choice);
    }
    while(choice =='Y');
}
Event.displayFinalResults();
Sign up to request clarification or add additional context in comments.

Comments

2

You could do this using break; as follows:

do {
    System.out.println("Is there anymore amounts you want to add?(Y/N): ");
    choice = keyboard.next().charAt(0);
    choice = Character.toUpperCase(choice);
    if (choice !='Y') {
        break;
    }

    readValidateAmountType();
    readValidateAmount(); 
} while(true);

Event.displayFinalResults();

Comments

0

One problem I see with this is, your while loop is inside the if statement. Once you exit the while loop, it's NOT going to run the code inside the else block because the if condition was already true.

So try removing the else block. This will make sure the Event.displayFinalResults method is called once the while loop exits.

     System.out.println("Is there anymore amounts you want to add?(Y/N): ");
        Scanner keyboard = new Scanner(System.in);
        char choice = keyboard.next().charAt(0);
        choice = Character.toUpperCase(choice);

if(choice == 'Y')
        {
            do
            {
                System.out.println("Is there anymore amounts you want to add?(Y/N): ");
                choice = keyboard.next().charAt(0);
                choice = Character.toUpperCase(choice);

                readValidateAmountType();
                readValidateAmount(); 
            }
            while(choice =='Y');

        }

        Event.displayFinalResults();

Comments

0

Clear code and compact:

Scanner keyboard = new Scanner(System.in);
char choice;
do {
   System.out.println("Is there anymore amounts you want to add?(Y/N): ");
   choice = keyboard.next().charAt(0);
   choice = Character.toUpperCase(choice);
   if(choice == 'Y') {
      readValidateAmountType();
      readValidateAmount(); 
   }
} while(choice == 'Y');     
Event.displayFinalResults();   

Comments

-1

Try the following out:

    public void answering(char answer){
        if(answer == 'Y' || answer == 'y'){
            System.out.println("Answering");
        } else if(answer == 'N' || answer == 'n'){
            System.out.println("Not answering");
    }

Instead of looping, call this method when you want to ask...

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.