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;
}
}
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 try to make a menu system, and I need to know how I can restart JTextField, so that by entering "x" number it shows me another menu, and you can continue browsing between the menus
JTextField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String Cap = JTextField.getText();
switch (Cap) {
case "1":
paintConsole("Menu1");
JTextField.setText("");
// In this site is my problem, I don't know how to reset a JTextField. PLEASE HELP ME
Cap = JTextField.getText();
if (Cap.equals("1")) {
paintConsole("Menu9");
}
break;
case "2":
paintConsole("Menu2");
break;
case "3":
paintConsole("Menu3");
break;
case "4":
paintConsole("Menu4");
case "5":
paintConsole("Menu5");
break;
case "6":
paintConsole("Menu6");
break;
}
}
});
}
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;
import java.util.Scanner;
public class ConvertLetterToNumber {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter one letter");
String str = input.next();
char ch = str.charAt(0);
switch (ch){
case 'A': case 'a': case 'B': case 'b': case 'C': case 'c':
System.out.println("the number is 2");
break;
case 'D': case 'd': case 'E': case 'e': case 'F': case 'f':
System.out.println("the number is 3");
break;
case 'G': case 'g': case 'H': case 'h': case 'I': case 'i':
System.out.println("the number is 4");
break;
case 'J': case 'j': case 'K': case 'k': case 'L': case 'l':
System.out.println("the number is 5");
break;
case 'M': case 'm': case 'N': case 'n': case 'O': case 'o':
System.out.println("the number is 6");
break;
case 'P':case 'p': case 'Q': case 'q': case 'R': case 'r': case 'S': case's':
System.out.println("the number is 7");
break;
case 'T':case 't':case 'U':case'u':case'V':case 'v':
System.out.println("the number is 8");
break;
case 'W':case 'w':case 'X': case 'x': case 'Y':case'y':case 'Z':case 'z':
System.out.println("the number is 9");
break;
default :System.out.println("Error:invalid input");
System.exit(1);
}
}
}
the problem is if I enter two or more letters at one time (for example "ab" the answer will be "2", but what I want is "Invalid input". How can I add this instruction to ask user to enter only one letter?
You could check the str length before your switch, and return like so:
if (str.length > 1){
return;
}
Here is a do..while loop with switch menu..
I am trying to make the user be able to return to the menu which is
System.out.println("Options: (C)reate, (R)ead, (W)rite, (L)ist, (S)ave or (E)xit. (*****)");
But after much trial and errors, I still cant get back to the main menu after choosing C, R, W, L or S.
Below is a short snippet of the codes..
do {
System.out.println("Options: (C)reate, (R)ead, (W)rite, (L)ist, (S)ave or (E)xit.
(*****)");
Scanner switchC = new Scanner(System.in);
System.out.println("Please enter your choice: ");
choice = switchC.next().charAt(0);
switch (Character.toUpperCase(choice)) {
case 'C':
System.out.println("C");
break;
case 'R':
break;
case 'W':
break;
case 'L':
break;
case 'S':
break;
case 'E':
System.exit(1);
default:
System.out.println("Options: (C)reate, (R)ead, (W)rite, (L)ist, (S)ave or (E)xit. (*****)");
}
} while(choice!='E');
try
do{
}
while(Character.toUpperCase(choice) !='E')