Hey guys my name is Tomas and i have just started programing in Java (currently studying Computer Studies at RMIT) and i need some help..
I have to create a console application with a list of options i have coded it as follows
now all that works. But the last part does not show when the user places in the wrong number but it loads the menu again (which is what i want but i also want it to display defult).
The other issue i am having with this code is when the user inputs a letter (A-Z, a-z) the following error.
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:857)
at java.util.Scanner.next(Scanner.java:1478)
at java.util.Scanner.nextInt(Scanner.java:2108)
at java.util.Scanner.nextInt(Scanner.java:2067)
at assignmentone.Main.main(Main.java:22)
Java Result: 1
please help. I would much appreciate it.
I have to create a console application with a list of options i have coded it as follows
Code:
public static void main(String[] args) {
// TODO code application logic here
Scanner scanner = new Scanner(System.in);
int selection;
while(true){
//Display the menu
System.out.println("Make a selection");
System.out.println("[1] Summery report for a sales employee");
System.out.println("[2] Print a pattern");
System.out.println("[3] Exit");
System.out.println(" Select option: ");
selection = scanner.nextInt();
switch (selection){
case 1:System.out.println("Loading report");
break;
case 2:System.out.println("Loading pattern");
break;
case 3:System.out.println("Exit Successful");
System.exit(0);
defult:System.out.println("Please enter a vaild selection:");
}//Switch
} //While
} //Main Static
now all that works. But the last part does not show when the user places in the wrong number but it loads the menu again (which is what i want but i also want it to display defult).
The other issue i am having with this code is when the user inputs a letter (A-Z, a-z) the following error.
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:857)
at java.util.Scanner.next(Scanner.java:1478)
at java.util.Scanner.nextInt(Scanner.java:2108)
at java.util.Scanner.nextInt(Scanner.java:2067)
at assignmentone.Main.main(Main.java:22)
Java Result: 1
please help. I would much appreciate it.