user2179615
user2179615

Reputation: 127

Why it keeps looping even i try to stop it?

I am supposed to write a program that asks the user "Do you love java?" and keep prompting the user for an answer.If the user enters "yes" or "no",print "thank you" and quit the program.Otherwise,print "try again" and then prompt user again.

import java.util.*;
public class Questionaire{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);

        System.out.print("Do you love java? > ");
        String output=sc.nextLine();

        while(!output.equalsIgnoreCase("yes") || !output.equalsIgnoreCase("no")){

            System.out.println("Try again!");
            System.out.print("Do you love java? > ");
            output=sc.nextLine();
            if(output.equalsIgnoreCase("yes") || output.equalsIgnoreCase("no")){
                System.out.println("Thank you!");
                break;
            }
        }

    }
}

Read through the comments and attempt it on my own again.

import java.util.*;
public class Questionaire{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);

        System.out.print("Do you love java? > ");
        String output=sc.nextLine();

        while(!output.equalsIgnoreCase("yes") || !output.equalsIgnoreCase("no")){
            if(output.equalsIgnoreCase("yes") || output.equalsIgnoreCase("no")){
                System.out.println("Thank you!");
                break;
            }

            System.out.println("Try again!");
            System.out.print("Do you love java? > ");
            output=sc.nextLine();

        }



    }
}

And i got it!!!Thanks people!

Upvotes: 0

Views: 134

Answers (5)

prashant
prashant

Reputation: 1815

while(!output.equalsIgnoreCase("yes") || !output.equalsIgnoreCase("no"))

condition should be changed to

while(!output.equalsIgnoreCase("yes") && !output.equalsIgnoreCase("no")){

Also you will have to add a if block outside your while to handle first input case.

Reason:

Case 1: Suppose you enter "ABC". Now

It is not equal to "yes" = true

It is not equal to "No" = True (This is not evaluated due to use of short circuit OR). This case works fine.

Case 2: Suppose you enter "Yes". now

It is not equal to "yes" = false

It is not equal to "No" = True.

By boolean Algebra : False OR True evaluates to True.Hence Indefinite loop.

Upvotes: 1

Arjun Rao
Arjun Rao

Reputation: 677

The best solution for your class is going to be:

import java.util.*;
public class Questionaire{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        System.out.print("Do you love java? > ");
        String output=sc.nextLine();
        while(true){ // Keep doing this forever
            if(output.equalsIgnoreCase("yes") || output.equalsIgnoreCase("no")){ // If the user enters yes or enters no
                System.out.println("Thank you!"); //Print thank you
                break; // Exit
            } else {
                System.out.println("Try again"); //Print try again
                output = sc.nextLine(); // Get new user input.
            }
        }
        sc.close();

    }
}

Upvotes: -1

Bhushan Bhangale
Bhushan Bhangale

Reputation: 10987

Try this simple approach where while loop runs until yes or no is entered. It will only break when user enters yes or no so print the Thank you! in the end.

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);

    System.out.print("Do you love java? > ");
    String output = sc.nextLine();

    while(!(output.equalsIgnoreCase("yes") || output.equalsIgnoreCase("no"))) {
        System.out.println("Try again!");
        System.out.print("Do you love java? > ");
        output = sc.nextLine();
    }

    System.out.println("Thank you!");
}

Upvotes: 1

Surendra Jnawali
Surendra Jnawali

Reputation: 3240

try it (It easier to handle then original program)

String output = "";
 while (!output.equalsIgnoreCase("yes") || !output.equalsIgnoreCase("no")) {
     System.out.print("Do you love java? > ");
     output = sc.nextLine();
     if (output.equalsIgnoreCase("yes") || output.equalsIgnoreCase("no")) {
         System.out.println("Thank you!");
         break;
     } else {
         System.out.println("Try again!");
     }
 }

Upvotes: 0

Nicknow
Nicknow

Reputation: 7234

Refactored the code, this should work for you. Using do-while the code will always execute once but if output is yes or no it does not loop through again.

public class Questionaire{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);

        String output;

        do{

            System.out.print("Do you love java? > ");
            output=sc.nextLine();

            if(output.equalsIgnoreCase("yes") || output.equalsIgnoreCase("no")){
                System.out.println("Thank you!");
            }
            else
            {
                System.out.println("Try again!");
            }

        }while(!output.equalsIgnoreCase("yes") && !output.equalsIgnoreCase("no"))

    }
}

Upvotes: 1

Related Questions