-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexception_2.java
More file actions
63 lines (58 loc) · 1.68 KB
/
exception_2.java
File metadata and controls
63 lines (58 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
Write a Java program to read n number of integers into an array.
Raise an appropriate Exception (ArithmeticException, InputMismatchException,
ArrayOutOfBoundsException) while performing following operations:
a) Dividing each element by the smallest element in an array.
b) Reading elements from the keyboard
c) Accessing the element from the index specified by the keyboard entry.
Concept of exception handling using multiple catch blocks to be used in this scenario.
*/
package Exceptions;
import java.util.*;
class MultiExcept {
private int arr[];
public void read() {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of elements");
int n = sc.nextInt();
arr = new int[n];
System.out.println("Enter "+ n +" elements");
for(int i = 0;i < n; i++) {
arr[i] = sc.nextInt();
}
}
public void divide() {
int small = arr[0];
for(int num:arr) {
if(num < small) {
small = num;
}
}
for(int num:arr) {
System.out.println(num + "/" + small + "=" + num / small);
}
}
public void getElement() {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the index to be accessed");
int idx = sc.nextInt();
System.out.println("Element at "+ idx +" is "+ arr[idx]);
sc.close();
}
}
public class exception_2{
public static void main(String[] args) {
MultiExcept ob1 = new MultiExcept();
try {
ob1.read();
ob1.divide();
ob1.getElement();
}catch(InputMismatchException e) {
System.out.println("Illegal Input");
}catch(ArithmeticException e) {
System.out.println("Smallest element is 0");
}catch(ArrayIndexOutOfBoundsException e) {
System.out.println("Illegal Index");
}
}
}