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;
Related
**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();
}
}
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.
So here is a link to another persons question and its my exact assignment.
https://stackoverflow.com/questions/39834840/concatenating-switch-statements-for-playing-cards-assignment
How can I get my result string to display the valueofCard then the suitofCard in this format "value of suit"
Also, how can I shorten up the 2-10 value range within a switch case?
I am aware I have " of " and a couple other lines that make no sense, I just figured I am missing something big anyways.
Thanks for any help, have lots of catching up to do.
System.out.print("Please enter a letter/integer of a playing card (A, J, Q, K, or 2 - 10),\nfollowed by card type (D, H, S, C):");
Scanner kbd = new Scanner(System.in);
String userInput = kbd.nextLine();
String valueofCard = userInput.substring(0, userInput.length() / 2); // gives first half of string
String suitofCard = userInput.substring(userInput.length() / 2); //give last half of string with + 1 if odd
StringBuilder result = new StringBuilder();
switch (valueofCard) {
case "A":
result.append("Ace of ");
break;
case "J":
result.append("Jack of ");
break;
case "Q":
result.append("Queen of ");
break;
case "K":
result.append("King of ");
break;
case "2":
result.append("2 of ");
case "3":
result.append("3 of ");
case "4":
result.append("4 of ");
case "5":
result.append("5 of ");
case "6":
result.append("6 of ");
case "7":
result.append("7 of ");
case "8":
result.append("8 of ");
case "9":
result.append("9 of ");
case "10":
result.append("10 of ");
break;
}
switch (suitofCard) {
case "D":
result.append("Diamonds");
break;
case "H":
result.append("Hearts");
break;
case "S":
result.append("Spades");
break;
case "C":
result.append("Clubs");
break;
}
System.out.println(result.toString());
kbd.close();
}
}
Option 1 - You could use a Stringbuilder to do this.
StringBuilder cardAndSuit = new StringBuilder();
switch (valueofCard) {
case "A":
cardAndSuit.append("Ace of");
.....
switch (suitofCard) {
case "D":
cardAndSuit.append("Diamonds");
break;
...
// Print the final string
System.out.println(cardAndSuit.toString()); // Ace of Diamonds
Option 2 - You could also just create a regular String and append to it.
String cardAndSuit = '';
cardAndSuit += "Ace of";
cardAndSuit += " Diamonds";
System.out.println(cardAndSuit); // Ace of Diamonds
2 is probably easier and more terse. Option 2 will actually be turned into option 1 by the compiler behind the scenes.
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;
}
}
I was wondering if anyone can see what is wrong with my code. It works except that the program is not acknowledging my switch statement - I searched lots of questions but as I am a novice I am clearly missing something.
import java.util.Scanner;
class Calmlr1 {
public static void main(String[]args) {
Scanner input = new Scanner(System.in);
String anotherOption = "y", operatorOpt= "a";
int no1=0, no2=0;
double result= 0;
System.out.println ("Welcome to the online calculator! Let's begin...");
while (anotherOption.equalsIgnoreCase ("y")) {
System.out.println ("Please enter your 1st number: ");
no1 = input.nextInt();
System.out.println ("Please confirm your operator:\n1 = +\n2 = - \n3 = *\n4 = /");
operatorOpt = input.next ();
System.out.println ("Please enter your 2nd number: ");
no2 = input.nextInt();
switch(no1) {
case 1:
result=no1+no2;
break;
case 2:
result=no1-no2;
break;
case 3:
result=no1*no2;
break;
case 4:
result=no1/no2;
default:
result = 0 ;
break;
}
System.out.println("Your total calculation is: "+result);
System.out.println("Would you like to do another sum? y/n ");
anotherOption=input.next();
}
}
}
You should be using switch(operatorOpt). Right now you are switching on the first number.
You also need to change:
int operatorOpt= 0;
operatorOpt = input.nextInt();
That is, if you want to keep your switch statement the same. Please also see #Daniel Imms answer for an additional bug fix.
Try adding a break at the end of case 4
case 4:
result=no1/no2;
break;
EDIT J L's answer is the main issue, but this is another problem that will break division.
Your switch should be on the operatorOpt and not on no1.
Also, you're missing a break in the case 4. So, if you want to do a division, you'll get 0 as result.
The input from the user for operatorOpt should be done with input.nextLine(). Or, if you want to keep the same switch statement, with input.nextInt().
It should be like this:
switch(operatorOpt)
{
case "+":
result=no1+no2;
break;
case "-":
result=no1-no2;
break;
case "*":
result=no1*no2;
break;
case "/":
result=no1/no2;
break;
default:
result = 0 ;
break;
}
Your switch statement should be on "operatorOpt" and not on "no1" as you suppose to check the operator and based on that you want to do the calculation. However, you must use JDK1.7 to use String in Switch statement since previous versions of JDK do not support String Switch.
Also, you should use "break" in case 4.
Your switch should be on the operatorOpt and not on no1.
You can use like this
switch(operatorOpt)
{
case "+":
result=no1+no2;
break;
case "-":
result=no1-no2;
break;
case "*":
result=no1*no2;
break;
case "/":
result=no1/no2;
break;
default:
result = 0 ;
break;
}