Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In this example program System.out.println() statements are used to display the

ID: 3576332 • Letter: I

Question

In this example program System.out.println() statements are used to display the menu and ask for a menu choice.

The menu choice is stored using an in.readLine() statement and then converting the String to an int.

Options for the user to choose as well as an option to exit the program are included.

1.Although our menu is not bad, think of some ways to make it more visually appealing, perhaps use tabs and spaces?

Post your thoughts on how to make a menu better looking, and also post what you think is a good example of a menu based on programs you have used.

2.What happens if we enter an option that is not supported by the menu? Can we crash the program with faulty input? Think of some ways to make your menu more stable.

Post your thoughts to the discussion board for others to read.

public class Menu public static void main (String args) throws IOException Create an Input Stream Buffered Reader in in new Buffered Reader (new InputStreamReader (System ln String input int choice loop until 4 is entered t the Menu System out println Main Menu System out println 1. Option 1 System.out println 2. Option 2 System. out println 3 Option 3 System out println Exit Program n Ask for choice System. out print What is your choice //get input input in. readLine Convert to an integer choice Integer. valueOf input int value(): while choice 4).

Explanation / Answer

I have updated the program. It now handles invalid inputs and doesnt crash after invalid input

import java.io.*;

public class Menu
{

public static void main(String a[])
{
String input;
int choice=0;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));


while(true) // Use loop to handle invalid inputs so that we can continue after exception
{
try
{
do
{
System.out.println("Main Menu");
System.out.println("Option 1:");
System.out.println("Option 2:");
System.out.println("Option 3:");
System.out.println("Option 4:");

System.out.println("Enter your choice");

input=in.readLine();

choice = Integer.valueOf(input).intValue();
}
while(choice!=4);
}
catch(IOException ie)
{

}
catch(NumberFormatException e) // Catch invalid input exception and print the appropriate message
{
System.out.println("Invalid Input! I said number");
}

if(choice==4) break;
}

}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote