0

I have a basic greeting program that is supposed to come up with a response based on the user's input. I am trying to accomplish it using for loops; however, whenever the user replies with the "bad" or "you" values stored in different arrays, the console prints out the same response multiple times. How could this problem be approached? I'm a noob here, and in java. I apologize if this answer has been previously answered. I searched but couldn't find. Thank you for your time.

import java.util.Scanner;

public class Greeter {
    public static void main(String[] args) {
        String[] greetings = { "well", "good", "great", "awesome", "fabulous" };
        String[] bad_greetings = { "bad", "awful" };
        String[] responses = { "you", "yourself" };

        System.out.println("Hello there, how are you?");
        String response;
        Scanner scan = new Scanner(System.in);
        response = scan.nextLine();
        for (String greeting : greetings) {
            if (response.contains(greeting)) {
                System.out.println("Well, good for you!");
            }
            for (String b_greet : bad_greetings) {
                if (response.contains(b_greet)) {
                    System.out.println("At least you have me.");
                }
            }
            for (String reply : responses) {
                if (response.contains(reply)) {
                    System.out.println("I'm well, thank you.");
                    // } else {
                    // System.out.println("Let's move on then.");
                    // }

                }
            }
        }

    }
}
Drakath12
  • 11
  • 4

3 Answers3

0

Change yout Main(). Your problem was that you have bad } for first loop also I recommand you break the cycle (or instead of break use return; from function) cause it is not neccesary to run all cycle if you already find your answer.

public static void main(String[] args) {
    String[] greetings = {"well", "good", "great", "awesome", "fabulous"};
    String[] bad_greetings = {"bad", "awful"};
    String[] responses = {"you", "yourself"};

    System.out.println("Hello there, how are you?");
    String response;
    Scanner scan = new Scanner(System.in);
    response = scan.nextLine();
    for (String greeting : greetings) {
        if (response.contains(greeting)) {
            System.out.println("Well, good for you!");
            break;//return;
        }
    }
    for (String b_greet : bad_greetings) {
        if (response.contains(b_greet)) {
            System.out.println("At least you have me.");
            break;//return;
        }
    }
    for (String reply : responses) {
        if (response.contains(reply)) {
            System.out.println("I'm well, thank you.");
            break;//return;
            // } else {
            // System.out.println("Let's move on then.");
            // }

        }
    }
}
Milkmaid
  • 1,659
  • 4
  • 26
  • 39
0

Milkmaid's answer is correct. To further elaborate on his response is that you are going to print your response 5 times (one for each item in your "greetings" array).

I can think of three options to solve your problem:

  1. keep what you have now, but use a break; statement to discontinue the for loop.
  2. Split up your for loops as Milkmaid did and include the break.
  3. Change your array to a list instead which has a built in "contains" method. How can I test if an array contains a certain value?

option 3 would be the cleanest :) You can convert it on the fly or just start with a list altogether. Seems like your program can just start with a list though.

Community
  • 1
  • 1
Cyrois
  • 439
  • 4
  • 9
0

You can try below

public static void main(String[] args) {
String[] greetings = {"well", "good", "great", "awesome", "fabulous"};
String[] bad_greetings = {"bad", "awful"};
String[] responses = {"you", "yourself"};

System.out.println("Hello there, how are you?");
String response;
Scanner scan = new Scanner(System.in);
response = scan.nextLine();
if(Arrays.asList(greetings).contains(response)){
      System.out.println("Well, good for you!");
      break;//return;
}
if(Arrays.asList(bad_greetings).contains(response)){
      System.out.println("At least you have me.");
      break;//return;
}
if(Arrays.asList(responses).contains(response)){
      System.out.println("I'm well, thank you.");
      break;//return;
}
 // } else {
 // System.out.println("Let's move on then.");
 // }

}