JAVA You will write a program that will allow the user to choose an exception fr
ID: 3702075 • Letter: J
Question
JAVA
You will write a program that will allow the user to choose an exception from the menu.
You will need try catch block to handle the exceptions. The user must be able to pick from the menu what exception they want r whether they are done. When the useer makes a choice the program will have a meethod that deals with the following:
1. ArrayIndexOutOfBounds
2.FileNotFoundException
3.InputMismatchException
4.ClassNotFoundException
5.OutOfMemoryException
6.NoSuchMethodException
7.Exit
- once selecting one of the above from the menu you will prompt the user to know whether they want to rn the program or break it.
-If they want to run the program tell them how to do so, and if they want to break it prompt them how to do so.
- outprint why it was succesful or why it broke.
- if exit is chosen outprint "Program is done, thankyou!"
***2 Files, one with exceptions and one with menu(main)***
If you have any questions please ask.
Explanation / Answer
Hello. Here is the required code you asked. Implemented everythings in a better and efficient way , letting user to input values that may or may not cause exceptions and display the outcomes. Comments are included. Drop a comment if you have doubts. Thanks.
// Exceptions.java
import java.io.File;
import java.io.FileNotFoundException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Exceptions {
static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
int choice = 0;
do {
/**
* printing menu
*/
System.out.println("1. ArrayIndexOutOfBounds");
System.out.println("2. FileNotFoundException");
System.out.println("3. InputMismatchException");
System.out.println("4. ClassNotFoundException");
System.out.println("5. OutOfMemoryException");
System.out.println("6. NoSuchMethodException");
System.out.println("7. Exit");
System.out.println("Enter your choice: ");
try {
choice = Integer.parseInt(scanner.nextLine());
} catch (NumberFormatException e) {
System.out.println("Not a valid choice");
continue;
}
/**
* performing appropriate demonstration, with letting user to choose
* to run causing the exception or not
*/
switch (choice) {
case 1:
try {
outOfBoundsDemo();
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("ArrayIndexOutOfBoundsException"
+ " is thrown!");
}
scanner.nextLine();
break;
case 2:
try {
openFileDemo();
} catch (FileNotFoundException e) {
System.out.println("FileNotFoundException" + " is thrown!");
}
break;
case 3:
try {
inputMismatchDemo();
} catch (InputMismatchException e) {
System.out
.println("InputMismatchException" + " is thrown!");
}
scanner.nextLine();
break;
case 4:
try {
classNotFoundDemo();
} catch (ClassNotFoundException e) {
System.out
.println("ClassNotFoundException" + " is thrown!");
}
break;
case 5:
try {
outOfMemoryDemo();
} catch (OutOfMemoryError e) {
System.out.println("OutOfMemoryError" + " is thrown!");
}
break;
case 6:
try {
noSuchMethodDemo();
} catch (NoSuchMethodException e) {
System.out.println("NoSuchMethodException" + " is thrown!");
}
break;
case 7:
System.out.println("Bye!");
}
} while (choice != 7);
}
/**
* method to demonstrate ArrayIndexOutOfBoundsException exception
*/
static void outOfBoundsDemo() throws ArrayIndexOutOfBoundsException {
int array[] = { 10, 20, 30, 40, 50, 60, 70 };
System.out.println("Array: " + Arrays.toString(array));
System.out.println("Enter a valid index to print the element,"
+ " enter invalid index to cause "
+ "ArrayIndexOutOfBoundsException: ");
int n = scanner.nextInt();
System.out.println(n + "th element is " + array[n]);
}
/**
* method to demonstrate FileNotFoundException exception
*/
static void openFileDemo() throws FileNotFoundException {
System.out.println("Enter file name, will throw "
+ "FileNotFoundException if not found: ");
String filename = scanner.nextLine();
Scanner fScanner = new Scanner(new File(filename));
System.out.println("File exists ");
}
/**
* method to demonstrate InputMismatchException exception
*/
static void inputMismatchDemo() throws InputMismatchException {
System.out.println("Enter an integer, anything else"
+ " to throw InputMismatchException: ");
int n = scanner.nextInt();
System.out.println("Entered integer is " + n);
}
/**
* method to demonstrate ClassNotFoundException exception
*/
static void classNotFoundDemo() throws ClassNotFoundException {
System.out.println("Enter a class name, will throw "
+ "ClassNotFoundException if not found");
String className = scanner.nextLine();
Class class1 = Class.forName(className);
System.out.println("Class found!");
}
/**
* method to demonstrate OutOfMemoryError exception
*/
static void outOfMemoryDemo() throws OutOfMemoryError {
System.out.println("Enter a String to add itself until"
+ " OutOfMemoryError is thrown: ");
String input = scanner.nextLine();
/**
* continuously looping and adding the string value to itself until Out
* of memory exception is thrown
*/
while (true) {
input = input + input;
}
}
/**
* method to demonstrate NoSuchMethodException exception
*/
static void noSuchMethodDemo() throws NoSuchMethodException {
System.out.println("Enter a method name of this class,"
+ " will throw NoSuchMethodException if not found");
String input = scanner.nextLine();
Method m = Exceptions.class.getDeclaredMethod(input, null);
System.out.println("specified method is found in this class");
}
}
/*OUTPUT (partial)*/
1. ArrayIndexOutOfBounds
2. FileNotFoundException
3. InputMismatchException
4. ClassNotFoundException
5. OutOfMemoryException
6. NoSuchMethodException
7. Exit
Enter your choice:
1
Array: [10, 20, 30, 40, 50, 60, 70]
Enter a valid index to print the element, enter invalid index to cause ArrayIndexOutOfBoundsException:
2
2th element is 30
1. ArrayIndexOutOfBounds
2. FileNotFoundException
3. InputMismatchException
4. ClassNotFoundException
5. OutOfMemoryException
6. NoSuchMethodException
7. Exit
Enter your choice:
1
Array: [10, 20, 30, 40, 50, 60, 70]
Enter a valid index to print the element, enter invalid index to cause ArrayIndexOutOfBoundsException:
15
ArrayIndexOutOfBoundsException is thrown!
1. ArrayIndexOutOfBounds
2. FileNotFoundException
3. InputMismatchException
4. ClassNotFoundException
5. OutOfMemoryException
6. NoSuchMethodException
7. Exit
Enter your choice:
3
Enter an integer, anything else to throw InputMismatchException:
5
Entered integer is 5
1. ArrayIndexOutOfBounds
2. FileNotFoundException
3. InputMismatchException
4. ClassNotFoundException
5. OutOfMemoryException
6. NoSuchMethodException
7. Exit
Enter your choice:
3
Enter an integer, anything else to throw InputMismatchException:
hsh
InputMismatchException is thrown!
1. ArrayIndexOutOfBounds
2. FileNotFoundException
3. InputMismatchException
4. ClassNotFoundException
5. OutOfMemoryException
6. NoSuchMethodException
7. Exit
Enter your choice:
4
Enter a class name, will throw ClassNotFoundException if not found
Abc
ClassNotFoundException is thrown!
1. ArrayIndexOutOfBounds
2. FileNotFoundException
3. InputMismatchException
4. ClassNotFoundException
5. OutOfMemoryException
6. NoSuchMethodException
7. Exit
Enter your choice:
7
Bye!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.