**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.
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.
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
I wanna Write a program to print out the user's grade ("Excellent if grade is A, Very Good if grade is B, Good if grade is C, Fair if grade is D, Fail if grade is F"). using Switch Statement
But When User input a or b or c or d or f in lowercase Apply the Grade
I mean I wanna char = A or a Match the Grade "Excellent"
and char = B or b Match the Grade " Very Good " and so on
Thanks and I hope to Find a Quick Solution
I tried this But it doesn't Make anything
public static void main(String[] args)
{
char Grade = 'A';
switch (Grade)
{
case 'A':
System.out.println("Excellent");
break;
case 'B':
System.out.println("Very Good");
break;
case 'C':
System.out.println("Good");
break;
case 'D':
System.out.println("Fair");
break;
case 'F':
System.out.println("Fail");
break;
}
}
You have two ways:
Multiple case statements:
switch (Grade) {
case 'A':
case 'a':
System.out.println("Excellent");
break;
case 'B':
case 'b':
System.out.println("Very Good");
break;
case 'C':
case 'c':
System.out.println("Good");
break;
case 'D':
case 'd':
System.out.println("Fair");
break;
case 'F':
case 'f':
System.out.println("Fail");
break;
}
}
Converting the character to upper case:
char Grade = 'A';
Grade = Character.toUpperCase(Grade);
switch (Grade) {
...
}
You can simply covert the character entered by the user to either lower case or lower case and compare accordingly.
I have my program figured out so far, it's just that I'm not understanding these instructions I was given (or at least understanding how to do them).
When I type 10, it prints out "10 of", but when I try to type 10S for 10 of Spades, it only prints out "Spades."
Hopefully, someone here can give me either a solution or point me in the right direction on how to solve my problem:
Use a SWITCH statement to assign the result variable an initial value - the value of the card
Use a second SWITCH statement to concatenate to the result variable the card's suit"
here is the code:
import java.util.*;
public class CardConverter {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
//will hold string that user will input
String card, face, suit, result;
//getting input from user and telling them correct format
System.out.println("Please enter either the number or face value intial of a card followed by the initial of it's suit (ie. QH for Queen of Hearts)");
card = keyboard.nextLine();
//gets first value
face = card.substring(0);
//sets substring for 10 only
//substring for after all single digit/letter card faces
suit = card.substring(1);
//to print face and word of
switch (face)
{
case "10":
System.out.println("10 of ");
break;
case "2":
System.out.println("2 of ");
break;
case "3":
System.out.println("3 of ");
break;
case "4":
System.out.println("4 of ");
break;
case "5":
System.out.println("5 of ");
break;
case "6":
System.out.println("6 of ");
break;
case "7":
System.out.println("7 of ");
break;
case "8":
System.out.println("8 of ");
break;
case "9":
System.out.println("9 of ");
break;
case "J":
System.out.println("Jack of ");
break;
case "Q":
System.out.println("Queen of ");
break;
case "K":
System.out.println("King of ");
break;
case "A":
System.out.println("Ace of ");
break;
}
//to print out card suit
switch (suit)
{
case "H":
System.out.println("Hearts");
break;
case "C":
System.out.println("Clubs");
break;
case "S":
System.out.println("Spades");
break;
case "D":
System.out.println("Diamonds");
break;
}
}
}
Your problem starts at card.substring(0);, which equals card because the substring from the start of the String. Maybe you wanted card.charAt(0);? But that is also wrong because "10S" will have three characters, two for the face value.
You'll need to handle a three-character input specially or be smarter about the substring-ing.
You know the suit will always be the last character, so use the length of the string to charAt for that.
int suitIndex = s.length() - 1;
String suit = ""+s.charAt(suitIndex);
String face = s.substring(0,suitIndex);
You can also simplify the cases
case "J":
System.out.println("Jack of ");
break;
case "Q":
System.out.println("Queen of ");
break;
case "K":
System.out.println("King of ");
break;
case "A":
System.out.println("Ace of ");
break;
default:
System.out.println(face + " of "); // handle all the numbers
break;
So i have created this code to convert words into phone numbers, but when i try to run this code with letters under 7 word it will display string index out of range. But 7 or more is fine. How do i fix this? If so how do i set the string range?
{
System.out.println("Enter a word to be converted: ");
String telLetter = console.next ();
telLetter = telLetter.toUpperCase();
String telNumber="7";
int count=0;
int i=0;
while(count <7)
{switch(telLetter.charAt(i))
{case 'A':case 'B':case 'C': case 'a': case 'b': case 'c':
telNumber += "2";
count++;
break;
case 'D':case 'E':case 'F': case 'd': case 'e': case 'f':
telNumber += "3";
count++;
break;
case 'G':case 'H':case 'I': case 'g': case 'h': case 'i':
telNumber += "4";
count++;
break;
case 'J':case 'K':case 'L': case 'j': case 'k': case 'l':
telNumber += "5";
count++;
break;
case 'M':case 'N':case 'O': case 'm': case 'n': case 'o':
telNumber += "6";
count++;
break;
case 'P':case 'R':case 'S': case 'p': case 'r': case 's':
telNumber += "7";
count++;
break;
case 'T':case 'U':case 'V': case 't': case 'u': case 'v':
telNumber += "8";
count++;
break;
case 'W':case 'X':case 'Y':case 'Z': case 'w': case 'x': case 'y': case 'z':
telNumber += "9";
count++;
break;
}
if( count==3) {
telNumber += "-";
}
i++;
}
System.out.println( telNumber );
}
}}
Fixes in code:
Use while(count < telLetter.length()) in place of while(count <7)...
telLetter.charAt(i) can be removed by (telLetter.charAt(count))... By doing this you don't need to create an extra variable int i = 0;... Its a good practice to keep variable minimum.
Use a scanner to get the input... Like.. Scanner sc = new Scanner(System.in); String telLetter = sc.next();...
After you used your resources do close them by using sc.close();....
Also i can see that you are appending 7 at the begining of every number... if that's a requirement then its ok.. other wise you can use... String telNumber="";
I would recommend using StringBuilder because in one object you can do it all.. While appending a character every time in string you are continuously creating a new string and giving it the reference of telNumber.
Also in your code either remove telLetter = telLetter.toUpperCase(); or remove the cases with lowercase alphabets.. because on making toUpperCase() you are just writing extra lines by writing cases for lowercase characters that are not needed...
System.out.println("Enter a word to be converted: ");
Scanner scan=new Scanner(System.in);
String telLetter = scan.next ();
int stringLength=telLetter.length();
telLetter = telLetter.toUpperCase();
String telNumber="7";
while(count <stringLength)
{
switch(telLetter.charAt(count))
Change your code like this.
But as per your code, the 7 is always prefixed in the converted phone number.
String telNumber=""; telNumber should be declared with empty string.
while(count <7)
Your code won't end until it has run through 7 characters. Try using while(count < telLetter.length())
Your while loop condition needs to check for the length of the telLetter string.
while (count <7 && count < telLetter.length())
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;
}
'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: // ...
// ...
}
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 */
}
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;
.
.
.
.
.
}
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;
}
If you make month as integer, then remove single quotes and it will work
else make month as string it will work.
For class I am attempting to design a menu driven program which has sub-menu's. I'm having some logic issues.
Originally I made it so that the switch driven menu's just called each other and the sub-menus would call back to the main menu and so on. I was told this would eventually blow the stack by going forward with calls and not going backwards. I would like to confirm this and perhaps sort out my loop logic.
My desire is that when a user hits r or x in the submenu week1 it bounces back and reprints the main menu. Currently the submenu just reprints with the switch statement not working. My guess is that the value of main menu is still set to 1, but if i call it and pass a new value aren't I taking the stack 1 deeper?
The first menu.
//method to print and navigate the main menu
public void mainMenu(String choice){
toOutput.printMenu("src/datastructures/menus/main.txt");
choice = input.myScanner.nextLine();
do {
switch (choice){
case "1": toBaseNumberSystems.week1();
break;
case "2": toOutput.printMenu("src/datastructures/menus/week2.txt");
break;
case "3": toOutput.printMenu("src/datastructures/menus/week3.txt");
break;
case "4": toOutput.printMenu("src/datastructures/menus/week4.txt");
break;
case "5": toOutput.printMenu("src/datastructures/menus/week5.txt");
break;
case "6": toOutput.printMenu("src/datastructures/menus/week6.txt");
break;
case "7": toOutput.printMenu("src/datastructures/menus/week7.txt");
break;
case "x": choice="x";
System.out.println("Goodbye");
break;
default: System.out.println("The character entered was not understood"
+ ", please try again");
mainMenu(choice);
break;
}
} while (!"x".equals(choice));
Calls only week1 menu, which in turn calls other menus.
public void week1(){
toOutput.printMenu("src/datastructures/menus/baseNumberSystems.txt");
choice = input.myScanner.nextLine();
do{
switch (choice){
case "1": toOutput.printMenu("src/datastructures/menus/week1Base8.txt");
week1Base8();
break;
case "2": toOutput.printMenu("src/datastructures/menus/week1Base5.txt");
week1Base5();
break;
case "3": toOutput.printMenu("src/datastructures/menus/week1Base4.txt");
week1Base4();
break;
case "4": toOutput.printMenu("src/datastructures/menus/week1Base2.txt");
week1Base2();
break;
case "r": choice="r";
break;
case "x": choice="x";
System.out.println("Goodbye");
break;
default: System.out.println("The character entered wass not understood"
+ ", please try again");
break;
}
}while (!"x".equals(choice)&&!"r".equals(choice));
}
//method to print and control the base8 menu
public void week1Base8(){
int base;
toOutput.printMenu("src/datastructures/menus/week1Base8.txt");
choice = input.myScanner.nextLine();
switch (choice){
case "1": base=8;
toBase(base);
break;
case "r": week1();
break;
case "x": choice="x";
System.out.println("Goodbye");
break;
default: System.out.println("The character entered was not understood"
+ ", please try again");
break;
}
}
// method to print and control the base5 menu
public void week1Base5(){
toOutput.printMenu("src/datastructures/menus/week1Base5.txt");
int base;
choice = input.myScanner.nextLine();
switch (choice){
case "1": base=5;
toBase(base);
break;
case "r": week1();
break;
case "x": choice="x";
System.out.println("Goodbye");
break;
default: System.out.println("The character entered was not understood"
+ ", please try again");
break;
}
}
//method to print and control the base4 menu
public void week1Base4(){
toOutput.printMenu("src/datastructures/menus/week1Base4.txt");
int base;
choice = input.myScanner.nextLine();
switch (choice){
case "1": base=4;
toBase(base);
case "r": week1();
break;
case "x": choice="x";
System.out.println("Goodbye");
break;
default: System.out.println("The character entered was not understood"
+ ", please try again");
break;
}
}
//method to print and control the base2 menu
public void week1Base2(){
toOutput.printMenu("src/datastructures/menus/week1Base2.txt");
int base;
choice = input.myScanner.nextLine();
switch (choice){
case "1": base=2;
toBase(base);
case "r": week1();
break;
case "x": choice="x";
System.out.println("Goodbye");
break;
default: System.out.println("The character entered was not understood"
+ ", please try again");
break;
}
}