-2
import java.util.Scanner;
public class DaysInMonth
{
public static void main(String[] args)
  {
  Scanner input = new Scanner(System.in);
  System.out.print("Enter a year:")
  int year = input.nextInt();    enter code here
  System.out.print("Enter a month:"); 
  int month = input.nextInt();    enter code here
  int days = 0;       
  boolean isLeapYear = (year % 4 == 0 && year % 100 != 0)||(year % 400 == 0);       

  switch (month){ 
  case 1: 
  days = 31; 

  break; 
  case 2: 
  if (isLeapYear) 
  days = 29; 
  else 
  days = 28; 

  break; 
  case 3: 
  days = 31; 
  break; 

 case 4: 
 days = 30; 
 break; 

 case 5: 
 days = 31; 
 break; 

 case 6: 
 days = 30; 
 break; 

  case 7: 
  days = 31; 
  break; 

  case 8 
  days = 31; 
  break; 

 case 9: 
 days = 30; 
 break; 

 case 10: 
 days = 31; 
 break; 

 case 11: 
 days = 30; 
 break; 

 case 12: 
 days = 31; 
 break; 
 default: 

String response = "Have a Look at what you've done and try again";
System.out.println(response); 
 System.exit(0); 
} 
  String response = "There are " +days+ " Days in Month "+month+ " of Year "   +year+ ".\n"; 
  System.out.println(response); // new line to show the result to the screen. 
} 

}

Why can't I type in January to get the same output result if I type 1? It should print "There are 31 days in the month of January of Year 2018" I initialized the month so it should read January or any other month.

I know I have int but I am wondering how can I also use January for 1 to get the same output.

2 Answers2

1

You can use Strings in a switch statement to check for several equivalent cases:

switch (monthInput.toLowerCase()) {
    case "january":
    case "jan":
    case "1":
        days = 31;
        break;

    case "february":
    case "feb":
    case "2":
        days = isLeapYear ? 29 : 28;
        break;

    case "march":
    case "mar":
    case "3":
        days = 31;
        break;

    // etc.

    default:
        System.out.println(monthInput + " is not a valid month");
        input.close();
        System.exit(0);
}

But this means you have to read your input as a String, not as an int...

Scanner input = new Scanner(System.in);
System.out.print("Enter a year:");
int year = input.nextInt();     // enter code here
input.nextLine(); // read the rest of the line (if any)

System.out.print("Enter a month:");
String monthInput = input.nextLine();

Note the use of input.nextLine(); after the .nextInt() — this is because the nextInt() call does not consume all of the input, it only reads the int that you typed for the Year, it does not read the newline (enter key), so you have to read that to be ready to read the next input, which is the month number or name.

Stephen P
  • 14,422
  • 2
  • 43
  • 67
0

I know I have int but I am wondering how can I also use January for 1 to get the same output.

A simple approach is to use name array

// before main.
static final String[] MONTH = "?,Jan,Feb,Mar,Apr,Jun,Jul,Aug,Sep,Oct,Nov,Dec".split(",");

// inside main
String monthStr = MONTH[month];

Add the full month names as needed.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130