Skip to content

Commit 066a3e0

Browse files
committed
Exception & input Output
1 parent 1e16db6 commit 066a3e0

31 files changed

+1821
-24
lines changed

Input & Output/001_Exception/.idea/misc.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Input & Output/001_Exception/.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Input & Output/001_Exception/.idea/workspace.xml

Lines changed: 95 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>
1.93 KB
Binary file not shown.
2.33 KB
Binary file not shown.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import java.util.InputMismatchException;
2+
import java.util.NoSuchElementException;
3+
import java.util.Scanner;
4+
5+
public class Example {
6+
7+
public static void main(String[] args) {
8+
try {
9+
int result = divide();
10+
System.out.println(result);
11+
} catch(ArithmeticException | NoSuchElementException e) {
12+
System.out.println(e.toString());
13+
System.out.println("Unable to perform division, autopilot shutting down");
14+
}
15+
}
16+
17+
private static int divide() {
18+
int x, y;
19+
// try {
20+
x = getInt();
21+
y = getInt();
22+
System.out.println("x is " + x + ", y is " + y);
23+
return x / y;
24+
// } catch(NoSuchElementException e) {
25+
// throw new ArithmeticException("no suitable input");
26+
// } catch(ArithmeticException e) {
27+
// throw new ArithmeticException("attempt to divide by zero");
28+
// }
29+
}
30+
31+
private static int getInt() {
32+
Scanner s = new Scanner(System.in);
33+
System.out.println("Please enter an integer ");
34+
while(true) {
35+
try {
36+
return s.nextInt();
37+
} catch(InputMismatchException e) {
38+
// go round again. Read past the end of line in the input first
39+
s.nextLine();
40+
System.out.println("Please enter a number using only the digits 0 to 9 ");
41+
}
42+
}
43+
}
44+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import java.util.InputMismatchException;
2+
import java.util.Scanner;
3+
4+
public class Main {
5+
6+
public static void main(String[] args) {
7+
// int x = 98;
8+
// int y = 0;
9+
// System.out.println(divideLBYL(x, y));
10+
// System.out.println(divideEAFP(x, y));
11+
// System.out.println(divide(x, y));
12+
int x = getIntEAFP();
13+
System.out.println("x is " + x);
14+
}
15+
16+
private static int getInt() {
17+
Scanner s = new Scanner(System.in);
18+
return s.nextInt();
19+
}
20+
21+
private static int getIntLBYL() {
22+
Scanner s = new Scanner(System.in);
23+
boolean isValid = true;
24+
System.out.println("Please enter an integer ");
25+
String input = s.next();
26+
for(int i=0; i<input.length(); i++) {
27+
if(!Character.isDigit(input.charAt(i))) {
28+
isValid = false;
29+
break;
30+
}
31+
}
32+
if(isValid) {
33+
return Integer.parseInt(input);
34+
}
35+
return 0;
36+
}
37+
38+
private static int getIntEAFP() {
39+
Scanner s = new Scanner(System.in);
40+
System.out.println("Please enter an integer ");
41+
try {
42+
return s.nextInt();
43+
} catch(InputMismatchException e) {
44+
return 0;
45+
}
46+
}
47+
48+
private static int divideLBYL(int x, int y) {
49+
if(y != 0) {
50+
return x / y;
51+
} else {
52+
return 0;
53+
}
54+
}
55+
56+
private static int divideEAFP(int x, int y) {
57+
try {
58+
return x / y;
59+
} catch(ArithmeticException e) {
60+
return 0;
61+
}
62+
}
63+
64+
private static int divide(int x, int y) {
65+
return x / y;
66+
}
67+
}
68+
8 KB
Binary file not shown.

Input & Output/002_InputOutput/.idea/misc.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)