Home > OS >  illegal line end in character literal
illegal line end in character literal

Time:04-06

**About the code: I am just making a simple code using a switch statement. All the switch cases work fine except the double-digit cases. I get an error saying :

year.java:37: error: unclosed character literal case '10'
year.java:40: error: unclosed character literal case '11':year.java:43: error: unclosed character literal case '12'

Code :

import java.util.Scanner;

public class year {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        char year;
        System.out.println("Enter the number of the month ");
        year = input.next().charAt(0);
        switch(year){
            case '1': 
            System.out.println("January");
            break;
            case '2':
            System.out.println("Febraury");
            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;
            default:
            System.out.println("Invalid");

        }
        input.close();

    }
}

I tried doing a few changes here and there but couldn't understand them and thus could not do so.

CodePudding user response:

Your variable year is a char. A char can only be a single character.

Therefore when you try and do '11' or '12' you run into issues as these "chars" consist of more than one character.

The quick solution here would be to use a String instead of char, using input.next() without the .charAt(0). Then you would need to change your case statements to use double quotes instead of single quotes.

Alternatively, you could do Integer.parseInt(input.next()) and then switch on an int instead, as @Tom has suggested.

CodePudding user response:

First of all, there is a Syntax error in case '12' it should be case '12': (Also the code indention is bad. Poor indentation make it hard for debugging)

I suggest you convert this code to a String based one. Please check the complete example below. Char can get one character only and char year; cause a bug, because in input values like 10, 11, 12 it won't work as expected

import java.util.Scanner;

public class year {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String year;
        System.out.println("Enter the number of the month ");
        year = input.nextLine();
        switch(year){
            case "1": 
            System.out.println("January");
            break;
            case "2":
            System.out.println("Febraury");
            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;
            default:
            System.out.println("Invalid");

        }
        input.close();
    }
}
  • Related