Modify before asshignment so that we can check input errors, etc.. Please refer
ID: 3926905 • Letter: M
Question
Modify before asshignment so that we can check input errors, etc.. Please refer to the following Java code.
You should add switch, run your programing untill you choose figure==0 (using while loop), cheak input error.
Catch (Exception e) is optional.
import java.util.Scanner;
public class inputError5 {
public static void main(String[] args) {
Scanner consol=new Scanner(System.in);
int choice=0;
while(true){
System.out.println("=============================");
System.out.println("= 1. function1 =");
System.out.println("= 2. function2 =");
System.out.println("= 3. function3 =");
System.out.println("=============================");
System.out.print("Select : ");
choice= consol.nextInt();
switch(choice){
case 0: System.out.println("Bye!! ");
System.exit(0);
case 1: function1();
break;
case 2: function2();
break;
case 3: function3();
break;
default:System.out.println("Wrong number!! Type 1~3 ");
break;
}
} // while
}
public static void function1(){
System.out.println("function1");
}
public static void function2(){
System.out.println("function2");
}
public static void function3(){
System.out.println("function3");
}
}
Explanation / Answer
Solution:
I am helping you by adding exception handling in this program. please find comments for more details...
package com.nancy.hello;
import java.util.InputMismatchException;
import java.util.Scanner;
public class inputError5 {
public static void main(String[] args) {
int choice = 0;
/**
* This while loop terminates when user enter value = 0 only.
*/
while (true) {
Scanner consol = new Scanner(System.in);
System.out.println("=============================");
System.out.println("= 1. function1 =");
System.out.println("= 2. function2 =");
System.out.println("= 3. function3 =");
System.out.println("=============================");
System.out.print("Select : ");
try {
choice = consol.nextInt();
// If user enters negative number then following error will be displayed and loop continues.
if (choice < 0) {
System.out.println("Error! Please enter a positive integer value.Type 1~3");
} else {
switch (choice) {
case 0:
System.out.println("You have entered 0, Bye!! ");
System.exit(0); // exiting from program
case 1:
function1();
break;
case 2:
function2();
break;
case 3:
function3();
break;
default:
System.out.println("Wrong number!! Type 1~3 ");
break;
}
}
} catch (InputMismatchException e) {
System.out.println("Input Error! Please enter a positive integer value.Type 1~3");
//continue;
}
} // while loop ends here
}
public static void function1() {
System.out.println("function 1");
}
public static void function2() {
System.out.println("function 2");
}
public static void function3() {
System.out.println("function 3");
}
}
Sample run:
=============================
= 1. function1 =
= 2. function2 =
= 3. function3 =
=============================
Select : nancy
Input Error! Please enter a positive integer value.Type 1~3
=============================
= 1. function1 =
= 2. function2 =
= 3. function3 =
=============================
Select : -1
Error! Please enter a positive integer value.Type 1~3
=============================
= 1. function1 =
= 2. function2 =
= 3. function3 =
=============================
Select : 1
function 1
=============================
= 1. function1 =
= 2. function2 =
= 3. function3 =
=============================
Select : 0
You have entered 0, Bye!!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.