0

I'm a beginner in Java, so I had a question on this I'm trying to create a simple code in which I'm done with it, just I wanted to know how do you set up an infinitely asking question that the user can input? The one I have right now does an infinite loop, which is not helpful...

    import java.util.Scanner;
    public class Logical_Operators {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
    
            Scanner input = new Scanner(System.in);
            
            //this is a conditional scenario, where only boys above the age of 18 can enter
            //and only girls equal to or over the age of 20 
            
            String gender, boy, girl, response;
            int age;
            
            System.out.println("Welcome to the party! Would you like to enter?");
            response = input.nextLine();
        
        while(!response.equals("yes") && !response.equals("no"))    {
            System.out.println("Please say Yes or No");
        }
        {
            
            if(response.equals("yes"))  {       
                
                System.out.println("What gender are you? Type boy or girl.");
                gender = input.nextLine();
                
                System.out.println("What about your age?");
                age = input.nextInt();
                
                    if(gender.equals("boy") && age >= 18)   {
                    System.out.println("You can enter, Welcome!"); 
                    
                }
                    else if(gender.equals("girl") && age >= 20) {
                    System.out.println("You can enter, Welcome!");  }
                 
                    else { System.out.println("Sorry, you may not enter because you don't meet the age requirements");  }
                
            }
            
            else if(!(response.equals("yes")||response.equals("no")))   {
                System.out.println("Please say Yes or No");
                
            }
            
            else { 
                System.out.println("Bye, hope to see you again!");
            }
            
        System.exit(1);
        }
            
        }
    
    }

Umair Mubeen
  • 796
  • 2
  • 18
  • 1
    Please fix the indentation of your source code, it makes it easier to read (specially for you). – Progman Oct 24 '20 at 21:22

2 Answers2

1

In order to get out of the loop, you need to get a new value to test

response = input.nextLine();

while(!response.equals("yes") && !response.equals("no"))    {
    System.out.println("Please say Yes or No");
    response = input.nextLine();
}
ControlAltDel
  • 28,815
  • 6
  • 42
  • 68
  • Hi, thanks for the response, it worked. But is there a way I can do the same for gender, and same for age? Like the infinite loop thing. I've tried using the same method but it just continues to ask the question even if I input boy or girl. – Gauraang Varshney Oct 25 '20 at 08:45
  • You should be able to use the exact same logic. Remember to replace "yes" and "no" with the new terms you are asking for – ControlAltDel Oct 25 '20 at 20:21
0
  1. You need to place the logic inside a loop which is currently not there in your code. In your code, only System.out.println("Please say Yes or No"); is inside the loop.
  2. You can use an infinite loop (i.e. while (true) { }) which you can break when the input matches the exit criteria.
  3. Also, I suggest you use equalsIgnoreCase instead of equals to test equality in a case-insensitive way.
  4. I also suggest you use age = Integer.parseInt(input.nextLine()) instead of age = input.nextInt() to avoid the problem discussed here.

Demo:

import java.util.Scanner;

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

        // this is a conditional scenario, where only boys above the age of 18 can enter
        // and only girls equal to or over the age of 20

        String gender, response;
        int age;

        while (true) {
            System.out.println("Welcome to the party! Would you like to enter?");
            System.out.print("Please say Yes or No: ");
            response = input.nextLine();
            if (response.equalsIgnoreCase("no")) {
                System.out.println("Bye, hope to see you again!");
                break;
            }

            if (response.equalsIgnoreCase("yes")) {
                System.out.print("What gender are you? Type boy or girl: ");
                gender = input.nextLine();

                System.out.print("What about your age?: ");
                age = Integer.parseInt(input.nextLine());

                if (gender.equalsIgnoreCase("boy") && age >= 18) {
                    System.out.println("You can enter, Welcome!");
                } else if (gender.equalsIgnoreCase("girl") && age >= 20) {
                    System.out.println("You can enter, Welcome!");
                } else {
                    System.out.println("Sorry, you may not enter because you don't meet the age requirements");
                }
            }
        }
    }
}

A sample run:

Welcome to the party! Would you like to enter?
Please say Yes or No: yes
What gender are you? Type boy or girl: boy
What about your age?: 24
You can enter, Welcome!
Welcome to the party! Would you like to enter?
Please say Yes or No: yes
What gender are you? Type boy or girl: girl
What about your age?: 19
Sorry, you may not enter because you don't meet the age requirements
Welcome to the party! Would you like to enter?
Please say Yes or No: yes
What gender are you? Type boy or girl: girl
What about your age?: 21
You can enter, Welcome!
Welcome to the party! Would you like to enter?
Please say Yes or No: no
Bye, hope to see you again!
Arvind Kumar Avinash
  • 50,121
  • 5
  • 26
  • 72