2

I'm brand new to coding and am trying to figure out why my else statement isn't working. I'm not sure if it is because of my boolean statement. I tried adding else as false for the boolean but that did not work.

import java.util.Scanner;
public class Practice {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Are you exercising tonight?");
        String answer = input.nextLine();
        System.out.println("You said " + answer + " Are you sure?");
        String answer2 = input.nextLine();
        boolean Yes = true;
        if (Yes) {
            System.out.println("Good. That's what I wanted to hear");
        } else {
            System.out.println("WRONG ANSWER");
        }
    }
}
  • Change Yes to false. – nicomp Jul 14 '21 at 12:50
  • 1
    It's not clear what you mean...you've set `Yes` as always being `true`, so the code will _always_ go into the `if` block and not the `else` block. What behaviour were you expecting instead, specifically? – ADyson Jul 14 '21 at 12:50
  • Maybe you meant to write `if(answer.equals("Yes") ) {` instead of hard-coding the boolean variable you've named `Yes`? Your code currently never actually checks what the user wrote in the input. – ADyson Jul 14 '21 at 12:51
  • Your boolean expression will always be true because the value of the variable "Yes" never changes; I'm not sure what you're expecting. Maybe you want to actually do something with the string input? – Dave Newton Jul 14 '21 at 12:51
  • Do you want to check if the user entered "yes"? If so, you do not need your `boolean Yes;` at all, you need to use one of your String variables (answer or answer2) and [compare them with your expected input](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – OH GOD SPIDERS Jul 14 '21 at 12:52
  • 1
    @DaveNewton corrected – ADyson Jul 14 '21 at 12:53
  • Thank you all! I was looking to have this statement appear if they wrote yes System.out.println("Good. That's what I wanted to hear"); and anything else written would write System.out.println("WRONG ANSWER") I will try if(answer.equals("Yes") ) as I think that is what I am looking for. – Timothy Williams Jul 14 '21 at 13:37

1 Answers1

1

You need to set your Yes variable to the condition you want to check:

import java.util.Scanner;

public class Practice {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        
        System.out.println("Are you exercising tonight?");
        String answer = input.nextLine();
    
        if (answer.equalsIgnoreCase("yes")) {
            System.out.println("Good. That's what I wanted to hear");
        } else {
            System.out.println("WRONG ANSWER");
        }
    }   
} 

If you want to make sure the user typed the right thing (the "Are you sure?" part), you can add a loop:

import java.util.Scanner;

public class Practice {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        
        String answer;
        while (true) {
            System.out.println("Are you exercising tonight?");
            answer = input.nextLine();

            System.out.println("You said " + answer + " Are you sure?");
            String answer2 = input.nextLine();

            // If the user is sure, break the loop
            if (answer2.equalsIgnoreCase("yes")) break;

            // Implicit else: ask the user again
        }
    
        if (answer.equalsIgnoreCase("yes")) {
            System.out.println("Good. That's what I wanted to hear");
        } else {
            System.out.println("WRONG ANSWER");
        }
    }   
} 
enzo
  • 9,861
  • 3
  • 15
  • 38