Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

207
Views
illegal line end in character literal

**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.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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.

over 4 years ago · Santiago Trujillo Report

0

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();
    }
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!