-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExceptiondemo1.java
More file actions
91 lines (75 loc) · 2.11 KB
/
Exceptiondemo1.java
File metadata and controls
91 lines (75 loc) · 2.11 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// exception handling basics
//you have to run your program in a happy condition then check for other conditions
//one by one start removing the exceptions
//same thing we do here
//1st make the program then try to remove the input mismatch exception
import java.util.InputMismatchException;
import java.util.Scanner;
class Input{
static int first =0;
static int second =0;
static Scanner scanner = new Scanner(System.in);
public int firstno(){
System.out.println("enter the 1st number");
try{
first = scanner.nextInt();
}
catch(InputMismatchException e){
System.out.println("enter the correct arithmetic number");
scanner.nextLine();
firstno();
}
return first;
}
public int secondno(){
System.out.println("enter the 2nd number");
try{
second = scanner.nextInt();
if(second == 0){
System.out.println("invalid input because no number is dividable by zero");
secondno();
}
}
catch(InputMismatchException e){
System.out.println("enter the correct arithmetic number");
scanner.nextLine();
secondno();
}
return second;
}
}
public class Exceptiondemo1 {
public static void main(String[] args) {
Input input = new Input();
int first = input.firstno();
int second = input.secondno();
// int first=0;
// int second = 0;
// Scanner scanner = new Scanner(System.in);
// System.out.println("enter the 1st number");;
// try{
// first = scanner.nextInt();
// }
// catch(InputMismatchException e){
// System.out.println("enter the arithmetic number");
// scanner.nextLine();
//
// }
// System.out.println("enter the 2nd number");
// try{
// second = scanner.nextInt();
// }
// catch(InputMismatchException e){
// System.out.println("enter the arithmetic number");
// }
int result = 0;
// try{
// result = first/second;
// }
// catch(ArithmeticException e){
// System.out.println("you are not giving correct input");
// }
result = first/second;
System.out.println("result is "+result);
}
}