0

I am trying to get user input for Yes or No (Y or N), but I keep running into complications. I also have to make it so that lowercase y and n work. I was wondering what the best way to go about this would be? Here is my program:

import java.util.Scanner;

public class Homework2 {
    public static void main(String args[]) {
        System.out.println("\nWould you like to see some healthy weight loss guidelines? (Y or N)");

        char decision = (char) System.in.read();

        if (decision.equals("Y")) {
            System.out.println("\nPlaceholder");
        } else if (decision.equals("N")) {
            System.out.println("\n");
            System.out.println("========================================================");
            System.out.println("||                                                    ||");
            System.out.println("||         Thank you From Your Friends At             ||");
            System.out.println("||          Happy Valley Fitness Center!              ||");
            System.out.println("||                                                    ||");
            System.out.println("========================================================");
        } else {
            System.out.println("\nYou did not enter yes or no, program stopping");
            System.out.println("\n");
            System.out.println("========================================================");
            System.out.println("||                                                    ||");
            System.out.println("||         Thank you From Your Friends At             ||");
            System.out.println("||          Happy Valley Fitness Center!              ||");
            System.out.println("||                                                    ||");
            System.out.println("========================================================");
        }
    }
}
Samuel Philipp
  • 9,552
  • 12
  • 27
  • 45
ChrisH811
  • 3
  • 2
  • Welcome to stack overflow! The Scanner class is great for this. Check out this question which has some good answers https://stackoverflow.com/questions/11871520/how-can-i-read-input-from-the-console-using-the-scanner-class-in-java – Eric Andrew Lewis Oct 13 '18 at 22:27

1 Answers1

0

Right now the problem is that you cannot call .equals() on a char. Instead simply read from System.in as a String and use the method .equalsIgnoreCase() so it will match either an upper case Y/N or lower case y/n:

Scanner in = new Scanner(System.in);       
String decision = in.next();
if(decision.equalsIgnoreCase("Y"))
{
    System.out.println("\nPlaceholder");
}
GBlodgett
  • 12,612
  • 4
  • 26
  • 42
  • 1
    Thank you so much! I feel like my biggest problem is I tend to over-complicate things with my code. It's working great now, again thanks! – ChrisH811 Oct 13 '18 at 21:01