user9431223
user9431223

Reputation:

Else loop in java

So I'm making a java program that will ask the user "how are you" and if the criteria in the if and if else statements isn't met then it should loop but i'm not sure how to implement the loop to make it so when the user doesn't input anything that is in both of the if statements it should repeat the code

Here is my code

import java.util.Scanner;

public class simplechatbot {

    private static Scanner scanner;

    public static void main(String args[]) {

        scanner = new Scanner(System.in);

        System.out.print("What is your name");
        String greeting = scanner.nextLine();
        System.out.print("Hi" + " " + greeting + ", ");

        System.out.print("How are you");
        String howareyou = scanner.nextLine();

        if (howareyou.equalsIgnoreCase("good")) {
            System.out.print("Thats good to hear!");
        } else if (howareyou.equalsIgnoreCase("not good")) {
            System.out.print("Unfortunate");
        } else {
             /*
                this is where I want to put the loop that goes back to asking the user
                "how are you" until they say something which matches the criteria
             */
        }
    }
}

Upvotes: 1

Views: 105

Answers (4)

Bacon
Bacon

Reputation: 1279

Use a while true loop forever unless one of the break conditions is met:

import java.util.Scanner;

public class SimpleChatBot {

    public static void main(String args[]) {
        try (Scanner scanner = new Scanner(System.in)) {
            while (true) {
                System.out.print("What is your name");
                String greeting = scanner.nextLine();
                System.out.print("Hi" + " " + greeting + ", ");

                System.out.print("How are you");
                String howareyou = scanner.nextLine();
                if (howareyou.equalsIgnoreCase("good")) {
                    System.out.print("Thats good to hear!");
                    break;
                } else if (howareyou.equalsIgnoreCase("not good")) {
                    System.out.print("Unfortunate");
                    break;
                }
            }
        }
    }
}

Upvotes: 1

leonardkraemer
leonardkraemer

Reputation: 6813

You can wrap everything in a while loop and then use the break statement to break out of the loop.

import java.util.Scanner;

public class simplechatbot{

    private static Scanner scanner;

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

        System.out.print("What is your name");
        String greeting = scanner.nextLine();
        System.out.print("Hi" + " " + greeting + ", ");
        while(true){
            System.out.print("How are you");
            String howareyou = scanner.nextLine();
            if(howareyou.equalsIgnoreCase("good");
               System.out.print("Thats good to hear!");
               break; // jumps to "here"
            }else if(howareyou.equalsIgnoreCase("not good"){
               System.out.print("Unfortunate");
               break; // jumps to "here"
            }
        } 
        // <- "here"; continue with the rest of your program at this line
    }
}

Upvotes: 0

Asaph
Asaph

Reputation: 162851

A do while loop should work nicely for this use case. Something like this:

String howareyou = null;
do {
    System.out.print("How are you");
    howareyou = scanner.nextLine();
while (!howareyou.equalsIgnoreCase("good") && !howareyou.equalsIgnoreCase("not good"));

if (howareyou.equalsIgnoreCase("good");
    System.out.print("Thats good to hear!");
} else {
     System.out.print("Unfortunate");
}

Upvotes: 0

ernest_k
ernest_k

Reputation: 45339

You could use a while loop which you only break from when your desired conditions are met, repeatedly prompting the user to type a desired value:

while(true) {
    if (howareyou.equalsIgnoreCase("good");
       System.out.print("Thats good to hear!");
       break;
    } else if (howareyou.equalsIgnoreCase("not good"){
        System.out.print("Unfortunate");
        break;
    }

    System.out.print("Please enter a valid response:");
    howareyou = scanner.nextLine();
}

Upvotes: 0

Related Questions