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')
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.
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;
}
New to Java and I'm having troubles with my code, it's a switch statement within a while loop. I like to use letters or "char" instead of numbered cases "int" and I have 'q' to quit. Thanks for your input. This is the main code.
import java.util.Scanner;
import java.util.*;
public class supraCritters {
public static void main(String [] arguments) {
Critter nastybat = new Critter();
nastybat.health = 100;
nastybat.mood = 50;
nastybat.hunger = 25;
System.out.println("Your critter has just been born,");
System.out.println("here are the stats of your critter.");
nastybat.checkStats();
System.out.println("\nPlease choose a letter");
System.out.println("[c]heck stats \n[f]eed \n[p]lay \n[r]ead \n[t]rain");
System.out.println("[q]uit");
Scanner sChoice = new Scanner(System.in);
char choice = ' ';
while (choice != 'q') {
switch (choice) {
case 'c':
nastybat.checkStats();
break;
case 'f':
nastybat.feed();
break;
case 'p':
nastybat.play();
break;
case 'r':
nastybat.read();
break;
case 't':
nastybat.train();
break;
case 'q':
System.out.println("good bye");
break;
default:
System.out.println("invalid entry");
break;
}
choice = sChoice.next().charAt(0);
}
}
}
When I enter corresponding letter the loop doesn't show Input method or repeat and 'q' does nothing. Default displays "invalid entry" before input.
Code edited and still have problems.
The input is taken only once, the first time! Therefore the loop always returns the same result. You should duplicate the getting input code inside the loop!
Scanner sChoice = new Scanner(System.in);
char choice = '';
while (choice != 'q') {
switch (choice) {
case 'c':
nastybat.checkStats();
break;
.
.
.
.
.
choice = sChoice.next().charAt(0);
The first line gets input for the first switch run, and the one inside the loop gets the rest.
UPDATE:
The choice = sChoice.next().charAt(0); should be place at the final of the loop, if not, as #proskor says, when user hits 'q' the program will return an 'invalid entry'.
I finished the code and it seems to work. Testing out the methods the object can use now.
Final
import java.util.Scanner;
import java.util.*;
public class supraCritters {
public static void main(String [] arguments) {
Critter nastybat = new Critter();
nastybat.health = 100;
nastybat.mood = 50;
nastybat.hunger = 25;
System.out.println("Your critter has just been born,");
System.out.println("here are the stats of your critter.");
nastybat.checkStats();
Scanner sChoice = new Scanner(System.in);
char choice = ' ';
while (choice != 'q') {
switch (choice) {
case 'c': case 'C':
nastybat.checkStats();
break;
case 'f': case 'F':
nastybat.feed();
break;
case 'p': case 'P':
nastybat.play();
break;
case 'r': case 'R':
nastybat.read();
break;
case 't': case 'T':
nastybat.train();
break;
case 'q': case 'Q':
System.out.println("good bye");
break;
default:
System.out.println("invalid entry");
break;
}
System.out.println("\nPlease choose a letter");
System.out.println("[c]heck stats \n[f]eed \n[p]lay \n[r]ead \n[t]rain");
System.out.println("[q]uit");
choice = sChoice.next().charAt(0);
}
}
}
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;
}
}