0

i'm a beginner at java and i'm trying to create a program that asks a user to enter a month, and prints the number of days in that month, as well as asking if it is a leap year. I've tried converting a String "yes" input from the scanner, into a boolean true or false input, however, i keep receiving the error cannot convert string into boolean. I've tried the parseString method however with no success. Any insight or help into this issue is greatly appreciated. Thanks for your time!

public static void main(String[] args){
    Scanner scan = new Scanner(System.in);
    int numberOfDays = 0;

    System.out.print("What's the month? ");
    String month = scan.nextLine();
    System.out.print("Is it a leap year? ");
    String leapYear = scan.nextLine();

    boolean b1 = Boolean.parseBoolean(leapYear);
    if (b1 = "yes") {
        b1 = true;
    }
    else {
        b1 = false;
    }

    switch(month) {
        case "January":                 
            numberOfDays = 31;
            break;              
        case "February":
            if (b1 = true) {
                numberOfDays = 29;
            } else {
                numberOfDays = 28;
            }
        break;
    }       
}   
utnicho
  • 49
  • 7
  • I might only ask about leap year if the user enters *February*. In other cases we don’t need the information. – Ole V.V. May 03 '20 at 06:45
  • 2
    While `if (b1 = "yes")` is wrong, as you have discovered, this one is really nasty: `if (b1 = true)`. It assigns true to `b1` and always evaluates to true! – Ole V.V. May 03 '20 at 06:46
  • If it’s an exercise, it’s a fine exercise. For production code one would leave it to the standard library: declare `DateTimeFormatter monthFormatter = DateTimeFormatter.ofPattern("LLLL", Locale.ENGLISH);` and then do `int numberOfDays = Month.from(monthFormatter.parse(month)).length(b1);`. – Ole V.V. May 03 '20 at 06:51

4 Answers4

2
boolean b1 = false;
if (leapyear.equals("yes")) {
    b1 = true;
}

I'm pretty sure Boolean.parseBoolean(leapYear) would only work if you entered "true" or "false".

Also, be careful using = in an expression.

if (b1 = "yes") {

probably will give you an error, but

if (b1 == "yes") {

is almost worse because it will only return true if b1 points to the exact same "yes" string object which it almost never will. Hence the use of .equals() which does what you expect.

D.L.
  • 314
  • 1
  • 7
1

You want something like this:

boolean b1 = "yes".equals(leapYear);

Remove the if else block with b1 = yes. You cant compare an String to a boolean, for comparison you need two equal signs, one would try to assign yes to a boolean, that's where your error comes from.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
TomStroemer
  • 1,390
  • 8
  • 28
1

Try the below code. some code correction needed I have marked the below.

public static void main(String[] args){
    Scanner scan = new Scanner(System.in);
    int numberOfDays = 0;

    System.out.println("What's the month? ");
    String month = scan.nextLine();
    System.out.print("Is it a leap year? ");
    String leapYear = scan.nextLine();

    boolean b1;
    if ("yes".equalsIgnoreCase(leapYear)) {
        b1 = true;
    }
    else {
        b1 = false;
    }

    switch(month) {
        case "January":                 
            numberOfDays = 31;
            break;              
        case "February":
            if (b1) {
                numberOfDays = 29;
            } else {
                numberOfDays = 28;
            }
        break;
    }
}
SSK
  • 3,444
  • 6
  • 32
  • 59
1

Your code is not compilable as you are assigning a String to boolean variable. That will not work. Try the below code

public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int numberOfDays = 0;

        System.out.print("What's the month? ");
        String month = scan.nextLine();
        System.out.print("Is it a leap year? ");
        String leapYear = scan.nextLine();

        boolean b1;
        if (leapYear.equalsIgnoreCase( "Yes")) {
            b1 = true;
        }
        else {
            b1 = false;
        }

        switch(month) {
            case "January":
                numberOfDays = 31;
                break;
            case "February":
                if (b1 == true) {
                    numberOfDays = 29;
                } else {
                    numberOfDays = 28;
                }
                break;
        }
    }

Edit: In Java single = is used for assignment and double = (==) is used for equality test. So in previous code the b = true reset the value of b1 to true irrespective of previous value set by your question of isLeapYear. and so else part is never executed.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Dinesh
  • 1,046
  • 1
  • 8
  • 17
  • this works perfect however when i input no for leap year so that b1 is false and thus numberofdays is assigned 28, i still get 29 for my output. I've tried else if b1 = false however to no success. I'm not sure why i keep getting 29. – utnicho May 03 '20 at 06:51
  • @utnicho I explained this in a comment to your question: *While `if (b1 = "yes")` is wrong, as you have discovered, this one is really nasty: `if (b1 = true)`. It assigns true to `b1` and always evaluates to true!* – Ole V.V. May 03 '20 at 07:12
  • 1
    Sorry, I did not notice in the code, so edited. In Java single = is used for assignment and doubel = (==) is used for equality test. So in previous code the b= true reset the value of b1 true irrespective of previous value set by your question of isLeapYear. and so else part is never exccuted, – Dinesh May 03 '20 at 07:46