pbars23
pbars23

Reputation: 95

Switch statement: Invalid character constant

I'm trying to create a switch statement that takes the month in as an integer, and based on that integer, I'd like to output a month name. For some reason that I don't know, the case '10' gives me an Invalid character constant error message. Does anyone know why this is happening and how I can solve this? Thanks, the code is below:

    switch (month) {
    case '1': System.out.println("January");
    break;
    case '2': System.out.println("February");
    break;
    case '3': System.out.println("March");
    break;
    case '4': System.out.println("April");
    break;
    case '5': System.out.println("May");
    break;
    case '6': System.out.println("June");
    break;
    case '7': System.out.println("July");
    break;
    case '8': System.out.println("August");
    break;
    case '9': System.out.println("September");
    break;
    case '10': System.out.println("October");
    break;
    case '11': System.out.println("November");
    break;
    case '12': System.out.println("December");
    break;
    }

After answer:

    switch (month) {
    case "1": System.out.println("January");
    break;
    case "2": System.out.println("February");
    break;
    case "3": System.out.println("March");
    break;
    case "4": System.out.println("April");
    break;
    case "5": System.out.println("May");
    break;
    case "6": System.out.println("June");
    break;
    case "7": System.out.println("July");
    break;
    case "8": System.out.println("August");
    break;
    case "9": System.out.println("September");
    break;
    case "10": System.out.println("October");
    break;
    case "11": System.out.println("November");
    break;
    case "12": System.out.println("December");
    break;
    }

after more answers:

    switch (month) {
    case 1: System.out.println("January");
    break;
    case 2: System.out.println("February");
    break;
    case 3: System.out.println("March");
    break;
    case 4: System.out.println("April");
    break;
    case 5: System.out.println("May");
    break;
    case 6: System.out.println("June");
    break;
    case 7: System.out.println("July");
    break;
    case 8: System.out.println("August");
    break;
    case 9: System.out.println("September");
    break;
    case 10: System.out.println("October");
    break;
    case 11: System.out.println("November");
    break;
    case 12: System.out.println("December");
    break;
    }

Upvotes: 0

Views: 3689

Answers (5)

pd30
pd30

Reputation: 240

If you make month as integer, then remove single quotes and it will work else make month as string it will work.

Upvotes: 1

You are having 2 characters after the 9

e.g. 10, 11, 12

So that can't be consider as a single char. That is why you are getting the error.

If you are using Java version 1.7 or above you can use string instead of char. But I think the best way is to cast month variable to a int and have int cases

//first cast month to a int
switch (month) {
    case 1: System.out.println("January");
         break;
    case 2: System.out.println("February");
         break;
    ......
    case 10: System.out.println("October");
         break;
    case 11: System.out.println("November");
         break;
    case 12: System.out.println("December");
         break;
}

Upvotes: 2

Mateusz Dymczyk
Mateusz Dymczyk

Reputation: 15141

In Java `` denotes a character whereas "" denotes a string. 10 is not a character in Java but two characters therefore you cannot place it there.

In Java 7 you can do a switch on Strings so you'd have to change all your `` to "" and month to a String like this:

switch(month) {
    case "1": // stuff
    /* rest */
}

Or drop the `` altogether and switch on int:

switch(month) {
    case 1: // stuff
    /* rest */
}

Upvotes: 3

user3921830
user3921830

Reputation: 61

As said by others '10' has 2 characters . Why don't you use integers instead :

 int month;
    switch(month){
    case 1:

    break;
    .
    .
    .
    }

From java 1.7 Strings are also allowed in switch statements so you can also write :

String month = // i/p

switch(month){
case "1" :

break;
.
.
.
.
.
}

Upvotes: 2

nem035
nem035

Reputation: 35501

'10' has two characters, i.e. a '1' and a '0'

Why don't you just use int instead of char for you switch statement variable...

int month = // ... however you get your month
switch(month) {
    case 1: // ...
    case 2: // ...
    case 3: // ...
    // ...
}

Upvotes: 4

Related Questions