This question has already been answered:

import java.util.Scanner; public class Classwork { public static void main(String args[] ) throws Exception { Scanner stdin = new Scanner(System.in); int year = stdin.nextInt(); String month = stdin.next(); boolean isLeapYear = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); if(isLeapYear == true && month == "Feb") System.out.print(month + " " + year + " has 29 days"); else if(isLeapYear == false && month == "Feb") System.out.print(month + " " + year + " has 28 days"); switch(month){ case "Jan": System.out.print(month + " " + year + " has 31 days"); break; case "Mar": System.out.print(month + " " + year + " has 31 days"); break; case "Apr": System.out.print(month + " " + year + " has 30 days"); break; case "May": System.out.print(month + " " + year + " has 31 days"); break; case "Jun": System.out.print(month + " " + year + " has 30 days"); break; case "Jul": System.out.print(month + " " + year + " has 31 days"); break; case "Aug": System.out.print(month + " " + year + " has 31 days"); break; case "Sep": System.out.print(month + " " + year + " has 30 days"); break; case "Oct": System.out.print(month + " " + year + " has 31 days"); break; case "Nov": System.out.print(month + " " + year + " has 30 days"); break; case "Dec": System.out.print(month + " " + year + " has 31 days"); break; } } } 

When I enter 2222 Feb, it displays an empty value. Tell me what is wrong and what needs to be fixed?

Marked as a duplicate by participants Enikeyschik , default locale , 0xdb , LFC , aleksandr barakin on Feb. 9 at 16:08 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • one
    It is not necessary to write in the title, what the program does, especially with such a long text, it does not interest anyone. It is necessary to briefly describe the problem. - Enikeyschik February

4 answers 4

  if(isLeapYear && month.equals("Feb")) System.out.print(month + " " + year + " has 29 days"); else if(!isLeapYear && month.equals("Feb")) System.out.print(month + " " + year + " has 28 days"); 

The point is that the strings must be compared via .equals, and not through ==.

    There is no need to make a separate code for February, you can safely integrate it into the switch:

     case "Feb": if (isLeapYear) { System.out.print(month + " " + year + " has 29 days"); } else { System.out.print(month + " " + year + " has 28 days"); } break; 

    In addition, months with the same number of days (30 or 31) can be grouped together:

     case "Jan": case "Mar": case "May": // итд System.out.print(month + " " + year + " has 31 days"); break; 

      Cannot compare strings with ==

      Use the java method - .equals("");


      Suppose there is a String str = "Hello, world!"; string String str = "Hello, world!";

      And you need to compare it:

        `if(str.equals("Hello, world!")){ System.out.println("Сравнение произошло успешно!"); }` 

      We do something like this. And all you have to earn

         Scanner stdin = new Scanner(System.in); int year = stdin.nextInt(); String month = stdin.next(); boolean isLeapYear = ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0); if(isLeapYear && month.equalsIgnoreCase("Feb")) System.out.print(month + " " + year + " has 29 days"); else if(month.equalsIgnoreCase("Feb")) System.out.print(month + " " + year + " has 28 days"); switch(month){ case "Jan": System.out.print(month + " " + year + " has 31 days"); break; case "Mar": System.out.print(month + " " + year + " has 31 days"); break; case "Apr": System.out.print(month + " " + year + " has 30 days"); break; case "May": System.out.print(month + " " + year + " has 31 days"); break; case "Jun": System.out.print(month + " " + year + " has 30 days"); break; case "Jul": System.out.print(month + " " + year + " has 31 days"); break; case "Aug": System.out.print(month + " " + year + " has 31 days"); break; case "Sep": System.out.print(month + " " + year + " has 30 days"); break; case "Oct": System.out.print(month + " " + year + " has 31 days"); break; case "Nov": System.out.print(month + " " + year + " has 30 days"); break; case "Dec": System.out.print(month + " " + year + " has 31 days"); break; }