0

I wrote a program for school, which is supposed to ask for the day of the week, whether or not it is vacation, and then print out whether the user can sleep in. However, the Boolean values, sleepInDay(which decides whether it is a weekday or not) and sleepInDayV (which decides whether or not it is a vacation) both automatically change to false, even when I change their default values to true at the beginning of the code.

import java.io.*;

class main {
public static void main(String[] args) {
// TODO Auto-generated method stub
try{
    InputStreamReader istream = new InputStreamReader(System.in);
    BufferedReader bufRead = new BufferedReader (istream);

    boolean sleepInDay;
    boolean sleepInDayV;

    System.out.println("Please enter the day of the day"  );
    String dayOfTheWeek = bufRead.readLine();

    if (dayOfTheWeek == "Sunday") {
       sleepInDay = true;
    } else if (dayOfTheWeek == "Saturday") {
       sleepInDay = true;
    } else {
       sleepInDay = false;
    }

    System.out.println("Is it vacation?"  );
    String Vacation = bufRead.readLine();

    if (Vacation == "Yes") {
       sleepInDayV = true;
    } else {
       sleepInDayV = false;
    };
    System.out.println(sleepInDay + " " + sleepInDayV);

    if ( sleepInDay == true || sleepInDayV == true) {
        System.out.println("You get to sleep in!");
    } else {
        System.out.println("GET UP!");
    }
} catch(IOException err) {
    System.out.println("Error reading line");{
}

Thanks for the help, Kitten Taco

sprinter
  • 27,148
  • 6
  • 47
  • 78
  • Eternal September. This is definitely a duplicate. – nhgrif Oct 19 '15 at 23:40
  • Compare strings using equals method and not ==. – Juned Ahsan Oct 19 '15 at 23:40
  • Don't compare Strings using `==` or `!=`. Use the `equals(...)` or the `equalsIgnoreCase(...)` method instead. Understand that `==` checks if the two *object references* are the same which is not what you're interested in. The methods on the other hand check if the two Strings have the same characters in the same order, and that's what matters here. So instead of `if (Vacation == "Yes") {`, use `if ("yes".equalsIgnoreCase(Vacation)) {`. – Hovercraft Full Of Eels Oct 19 '15 at 23:47
  • As an aside, you will want to learn and use [Java naming conventions](http://en.wikipedia.org/wiki/Naming_convention_(programming)#Java). Variable names should all begin with a lower letter while class names with an upper case letter. Learning this and following this will allow us to better understand your code, and would allow you to better understand the code of others. – Hovercraft Full Of Eels Oct 19 '15 at 23:47

0 Answers0