my program is presenting menu which present a list of avialable option(A-B-C)
if the user enter an invalid option the program display error massage
The problem is where i enter the invalide choice 2 times in while loop or when i enter available choice
in the firest time then i chose invalide choice
import java.io.IOException;
import java.text.*;
import java.util.*;
public class choice {
Scanner reader=new Scanner(System.in);
char choice ='\0';
do
{
System.out.println( Menu");
System.out.println("A.Checking ");
System.out.println("B. saving .");
System.out.println("C.Exiting .");
System.out.print(" enter choice ");
try
{
choice = (char)System.in.read();
System.in.read(); //eat the '\n'
}
catch (IOException e)
{
e.printStackTrace();
}
if(choice=='A' || choice == 'a'){
System.out.println("A");
}
else if(choice=='B' || choice == 'b') {
System.out.print("b:");
}
else if (choice == 'C' || choice == 'c') {
System.out.println("exit");
}
else {
System.out.println("Error!"+" "+ choice +" "+"is not a valid option!");
}
System.out.println();
}while (choice != 'C' && choice != 'c');
}
}
This is the output
Menu A.Checking B.Basic saving calculator. X.Exit the program. enter choice:a A Menu A.Checking B.Basic saving calculator. X.Exit the program. enter choice:b B Menu A.Checking B.Basic saving calculator. X.Exit the program. enter choice: r Error! r is not a valid option! Menu A.Checking B.Basic saving calculator. X.Exit the program. enter choice: o Error! is not a valid menu option! Menu A.Checking B.Basic saving calculator. X.Exit the program. enter choice: Error! is not a valid menu option! Menu A.Checking B.Basic saving calculator. X.Exit the program. enter choice: w Error! w is not a valid menu option! Menu A.Checking B.Basic saving calculator. X.Exit the program. enter choice: w Error! is not a valid menu option! Menu A.Checking B.Basic saving calculator. X.Exit the program. enter choice: (i print c put it dose not appeare) Error! is not a valid menu option! Menu A.Checking B.Basic saving calculator. X.Exit the program. enter choice: r Error! r is not a valid menu option! Menu A.Checking B.Basic saving calculator. X.Exit the program. enter choice: C Error! is not a valid menu option! Menu A.Checking B.Basic saving calculator. X.Exit the program. enter choice: Error! is not a valid menu option! Menu A.Checking B.Basic saving calculator. X.Exit the program. enter choice:C exiting

New Topic/Question
Reply




MultiQuote




|