

DownloadStatus ds DownloadStatus.values () downloadStatus. Then, to get your enum from the ordinal, you can do: int downloadStatus. I'm try to set up Try and Catch Exception so that if a user enters a String rather than an Int, the program will tell the user this is not valid and prompt them to try again. Every Java enum has an ordinal which is automatically assigned, so you don't need to manually specify the int (but be aware that ordinals start from 0, not 1). * Read the first character of the next line of text.I've got a main menu I'm creating for a program using a switch statement. Im not saying simplification is the only use for switches though.
RETURN VALUE IN SWITCH CASE JAVA CODE
It makes using switches to simplify code pointless since you always have to do a null check. Scanner.nextLine() // read the "\n" as well Im not understanding why a short circuit of null being mapped to the 'default' case or a special case for a null switch was not implemented for Strings. * be directly invoked on the class itself. * The constructor is private because no instances of this Private static final Scanner scanner = new Scanner(System.in) * A singleton instance of Scanner used for reading all input * All methods consume the end-of-line character. * This class provides static methods for easily reading Read choice method and an In class that is provided to us to use and when I try to return it still doesnt print out the main menu options. From Junilu Lacar's comment, I tried to put the menu options inside the while loop, however since it checks if the input is not 'X' first, it asks for a choice first but doesn't print out the menu options. This is in effect somewhat similar to a Java if statement, although the Java switch statement offers a somewhat more compressed syntax, and slightly different behaviour and thus possibilities. When you exit the loop, it will go back to the main menu loop but because the main menu options are printed outside the loop, the options are only printed the first time. A Java switch statement enables you to select a set of statements to execute based on the value of some variable. So I tried making the submenu a loop so the exit was 'R'. Welcome to the Catalogue! Please make a selection from the menu: The first reason is that you have all your code in your main function.

At this point your code isnt very testable, which is to say you can test it but it wont be the best tests.

Welcome to the Library! Please make a selection from the menu: The first step to writing unit tests is writing testable code. Thanks for helping.īelow is the output when I run the main method. So I test to go to the next menu and works fine, but when I try to go back, it doesn't work. Basically I've got a loop for the main menu when the user input is not 'X', then it will run the cases depending on what option you choose. It will ask me to input again for the catalogue menu and when I input 'R' which is the exit, it should go back and display the all the options for the first menu, but it doesn't. So it starts with a menu, and I input 1 which takes me to the next menu. Return to previous menu.") Ĭase '1' : catalogue.displayBooks() break Ĭase '2' : catalogue.displayAllBooks() break Ĭase '3' : catalogue.displayGenres() break Ĭase '4' : catalogue.booksInGenre() break Ĭase '5' : catalogue.displayAuthors() break Ĭase '6' : catalogue.booksByAuthor() break Ĭase '8' : catalogue.returnBook() break ("Welcome to the Catalogue! Please make a selection from the menu:") ("Welcome to the Library! Please make a selection from the menu:") I would appreciate it a lot if you could help me out. Ive looked up many solutions online but I still cant figure out what im doing wrong. Right now, whenever I try to return to the first menu, it wont show any of te options. Im having some trouble with returning to my previous menu.
