Joe
Joe

Reputation: 7

Java switch not working

Im having trouble getting my switch working. I get an uncompilable source code Date.java:75. Also, when the program returns the results - I would like it to also return MONTH NAME DD, YYYY along with what I already have it doing MM/DD/YYYY. If you could point me in the right direction, I'd appreciate it.

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Scanner;

public class Date {

private GregorianCalendar date = null;

private String[] months = new String[]{ "january", "february", "march", "april", "may",
                                "june", "july", "august", "september", "october", "november", "december" };



public Date(int month, int day, int year) {

    date = new GregorianCalendar(year, month-1, day);

}

public Date(String month, int day, int year) {

    date = new GregorianCalendar(year, this.getMonth(month), day);

}



public Date(int dayOfYear, int year) {

    date = new GregorianCalendar();

    date.set(Calendar.DAY_OF_YEAR, dayOfYear);

    date.set(Calendar.YEAR, year);

}


private int getMonth(String month) {

    for (int i=0; i<months.length; ++i)

      if (month.toLowerCase().equals(months)) //equals(months))

        return i;

    return 0;

}

public String toString() {

    return date.get(Calendar.MONTH)+1 + "-" + date.get(Calendar.DATE) + "-" + date.get(Calendar.YEAR);

}



public static void main(String[] args) {

    int mo;
  int dy;
  int yr;
  String moo; // Month name string

  boolean wrongInput = false;
  do {
      Scanner input = new Scanner( System.in ); // scanner to read input  
      wrongInput = false;
      int menu = input.nextInt(); // menu selection

          System.out.printf( "Enter 1 for format: MM/DD/YYYY \n");
            System.out.printf( "Enter 2 for format: Month DD,YYYY \n");    
             System.out.printf( "Enter 3 to exit \n");
          System.out.printf( "Choice:");


  switch(menu)
  {
      case '1' : // MM/DD/YYYY UI
        System.out.printf( "Enter Month (1-12): ");
        mo = input.nextInt();
        System.out.printf( "Enter Day of Month: ");
        dy = input.nextInt();
        System.out.printf( "Enter Year: ");
        yr = input.nextInt();

        Date a = new Date(mo, dy, yr); //chew
        System.out.println(a); //spit
          break;

      case '2' : // Month DD,YYYY UI
        System.out.printf( "Enter Month name: ");
        moo = input.next();
        System.out.printf( "Enter Day of Month: ");
        dy = input.nextInt();
        System.out.printf( "Enter Year: ");
        yr = input.nextInt();

        Date b = new Date(moo, dy, yr); //chew
        System.out.println(b); //spit
          break;

      case '3' : // EOP
        System.exit(0);  
          break;

      default:
       System.out.println("Invalid selection.");
       wrongInput = true;
          break;
        }
  while(wrongInput);
  } 

Upvotes: 0

Views: 1234

Answers (3)

Gumbo
Gumbo

Reputation: 1746

The problem is that in your switch you switch over an integer, but you have chars to define the single cases. (Chars are always noted with ''). the reason this does not instantly throw an compile error is that a char in java is saved as a normal integer. You can even perform math with chars. So the switch compares the integer menu to the integer equivalent of '1', '2' and so on. This could work, but for some reason the char '0' has an integer value of 40 (or 41?). So if you set menu to 40 (41?), case '1': will be triggered.

To avoid this simply change the code from case '1': case '2': and so on to case 1: case 2:` etc.

Upvotes: 1

Akash Thakare
Akash Thakare

Reputation: 22972

You are passing integer and checking for character. 1 is int while '1' is character in java.

In your code menu is int not char so your cases should be case 1: and not case '1':

So change your switch

 switch(menu){
      case 1:
      //Your code
      break;

      case 2:
      //Your code
      break;
      //..And so on
  }

Right now the ASCII values of characters '1','2'.. are compared with int value passed as menu which are obviously not going to be equal as ASCII value of char '1' is 81 while 82 for '2' so it is totally useless in this scenario.

Upvotes: 6

RaunaqSuri
RaunaqSuri

Reputation: 86

You're checking for characters, '1', '2', etc. Try just the number. E.g.

case 1:
//do this
break;

Upvotes: 3

Related Questions