1

How to create in Java solution just with "switch and if" such an input "January", "january", JANUARY", janUARY" and any other combination of case is treated as "January" and in all cases prints out 1. I did program, but it is working just with the same input words, maybe exist a trick to resolve that.

import java.util.Scanner;
    public class nameMonth
    {

       public static void main(String[] args)

       {
          String month;
          int nrMonth;

      Scanner keyboard = new Scanner(System.in);
      System.out.println("Enter a month");
      month = keyboard.nextLine();
      switch (month)
      {
         case "January":
            System.out.println("Your month is January");
            nrMonth = 1;
            break;

         case "February":
            System.out.println("Your month is February");
            nrMonth = 2;
            break;

         case "March":
            System.out.println("Your month is March");
            nrMonth = 3;
            break;

         case "April":
            System.out.println("Your month is April");
            nrMonth = 4;
            break;

         case "May":
            System.out.println("Your month is May");
            nrMonth = 5;
            break;

         case "June":
            System.out.println("Your month is June");
            nrMonth = 6;
            break;

         case "July":
            System.out.println("Your month is July");
            nrMonth = 7;
            break;

         case "August":
            System.out.println("Your month is August");
            nrMonth = 8;
            break;

         case "September":
            System.out.println("Your month is September");
            nrMonth = 9;
            break;

         case "October":
            System.out.println("Your month is October");
            nrMonth = 10;
            break;

         case "November":
            System.out.println("Your month is November");
            nrMonth = 11;
            break;

         case "December":
            System.out.println("Your month is December");
            nrMonth = 12;
            break;

         default:
            System.err.println("Your month is wrong");
            break;
      }
   }

}
LuCio
  • 5,055
  • 2
  • 18
  • 34
Dima Bors
  • 40
  • 7

6 Answers6

3

Use this:

switch (month.toUpperCase()) {

Then, the cases should be in uppercase, such as:

    case "JANUARY":
        System.out.println("Your month is January");
        nrMonth = 1;
        break;
Chai T. Rex
  • 2,972
  • 1
  • 15
  • 33
1

Unless you're required to use a switch for some reason, you could do something like this:

private int getMonthInYear(string month) {    
    string[] monthsInYear = {"JANUARY", "FEBRUARY", "MARCH", "APRIL", ..., "DECEMBER" };

    for (int i = 0; i < monthsInYear.length; i++) {
        if (monthsInYear[i].equalsIgnoreCase(month))
            return i+1; // Compensate for 0 indexing
    }    
    return -1; // In case it's not found
}

Then just call this method in your main. In general, sounds like you're looking for the equalsIgnoreCase(string s) string function or something similar.

Grey Haven
  • 380
  • 2
  • 13
0

You can do this.

      String month;
      int nrMonth;
      month.toUpperCase(); // Convert to Upper Case.

  Scanner keyboard = new Scanner(System.in);
  System.out.println("Enter a month");
  month = keyboard.nextLine();
  switch (month)
  {
     case "JANUARY": //Change all to caps.
        System.out.println("Your month is January");
        nrMonth = 1;
        break;

///ETC...
0

You can use this

String withCase = month.substring(0, 1).toUpperCase()+month.substring(1).toLowerCase();

switch (withCase) {
  case "January":
    System.out.println("Your month is January");
    nrMonth = 1;
    break;
user12222
  • 33
  • 6
0

You can do this

import java.util.Scanner;
public class nameMonth {

   public static void main(String[] args) {
       String month;
       int nrMonth;

       Scanner keyboard = new Scanner(System.in);
       System.out.println("Enter a month");
       month = keyboard.nextLine();

       if("January".equalsIgnoreCase(month)) {
          System.out.println("Your month is January");
          nrMonth = 1;
       }
       else if("February".equalsIgnoreCase(month)) {
          System.out.println("Your month is February");
          nrMonth = 2;
       }
       else if("March".equalsIgnoreCase(month)) {
          System.out.println("Your month is March");
          nrMonth = 3;
       }
       else if("April".equalsIgnoreCase(month)) {
          System.out.println("Your month is April");
          nrMonth = 4;
       }
       else if("May".equalsIgnoreCase(month)) {
          System.out.println("Your month is May");
          nrMonth = 5;
       }
       else if("June".equalsIgnoreCase(month)) {
          System.out.println("Your month is June");
          nrMonth = 6;
       }
       else if("July".equalsIgnoreCase(month)) {
          System.out.println("Your month is July");
          nrMonth = 7;
       }
       else if("August".equalsIgnoreCase(month)) {
          System.out.println("Your month is August");
          nrMonth = 8;
       }
       else if("September".equalsIgnoreCase(month)) {
          System.out.println("Your month is September");
          nrMonth = 9;
       }
       else if("October".equalsIgnoreCase(month)) {
          System.out.println("Your month is October");
          nrMonth = 10;
       }
       else if("November".equalsIgnoreCase(month)) {
          System.out.println("Your month is November");
          nrMonth = 11;
       }
       else if("December".equalsIgnoreCase(month)) {
          System.out.println("Your month is December");
          nrMonth = 12;
       }
       else {
          System.err.println("Your month is wrong");
       }  
   }
 }
pierDipi
  • 1,388
  • 1
  • 11
  • 20
0

You can simply use this line of code at the beginning (right below month = keyboard.nextLine();) and you will not have to change anything in your code.

month = month.substring(0,1).toUpperCase().concat(month.substring(1).toLowerCase());
Rarblack
  • 4,559
  • 4
  • 22
  • 33