Skip to content

Commit d7ed67b

Browse files
author
ashishyadav2
committed
add scanner
1 parent e508a59 commit d7ed67b

File tree

3 files changed

+102
-8
lines changed

3 files changed

+102
-8
lines changed
Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
package Exceptions;
22

33
class MyException extends Exception {
4-
public MyException(String message){
5-
super(message);
4+
public MyException(String msg){
5+
super(msg);
66
}
77
}
88
public class CustomExceptions {
99
public static void main(String[] args) {
10-
int a = 15;
11-
try{
12-
if( a>10) {
13-
throw new MyException("something went wrong");
14-
}
10+
int a = 5;
11+
try {
12+
throw new MyException("cannot divide by zero");
1513
}
1614
catch (MyException e){
1715
e.printStackTrace();
18-
System.out.println(e.getMessage());
16+
System.out.println("Exception occurred");
1917
}
2018
}
2119
}

Java2/Temp/pra2/InputPractice.java

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package Temp.pra2;
2+
3+
import java.io.File;
4+
import java.io.FileNotFoundException;
5+
import java.util.Scanner;
6+
class TClass {
7+
int n;
8+
int id;
9+
String name;
10+
int iter;
11+
String s;
12+
int p;
13+
int q;
14+
String r;
15+
int s2;
16+
17+
@Override
18+
public String toString() {
19+
return "TClass{" +
20+
"n=" + n +
21+
", id=" + id +
22+
", name='" + name + '\'' +
23+
", iter=" + iter +
24+
", s='" + s + '\'' +
25+
", p=" + p +
26+
", q=" + q +
27+
", r='" + r + '\'' +
28+
", s=" + s +
29+
'}';
30+
}
31+
}
32+
public class InputPractice {
33+
public static void main(String[] args) {
34+
File fileObj = new File("C:\\Users\\ashis\\IdeaProjects\\JavaLessons\\src\\Temp\\pra2\\input.txt");
35+
Scanner sc = null;
36+
try {
37+
sc = new Scanner(fileObj);
38+
int n = sc.nextInt();
39+
TClass[] arr = new TClass[n];
40+
for(int i=0;i<n;i++){
41+
double id = sc.nextDouble();
42+
sc.nextLine();
43+
String name = sc.nextLine();
44+
int iter = sc.nextInt();
45+
String s = "";
46+
int p = 0;
47+
for(int j=0;j<iter;j++) {
48+
sc.nextLine();
49+
s = sc.nextLine();
50+
p = sc.nextInt();
51+
System.out.println(s+ " "+p);
52+
}
53+
arr[i] = new TClass();
54+
System.out.println(id+" "+name+" "+iter);
55+
}
56+
int q = sc.nextInt();
57+
sc.nextLine();
58+
String r = sc.nextLine();
59+
int s2 = sc.nextInt();
60+
System.out.printf("%d %s %d",q,r,s2);
61+
}
62+
catch (FileNotFoundException e){
63+
System.out.println("file not found");
64+
}
65+
finally {
66+
assert sc != null;
67+
sc.close();
68+
}
69+
}
70+
}

Java2/Temp/pra2/input.txt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2
2+
2001.0
3+
hello
4+
4
5+
world
6+
5000
7+
alpha
8+
6000
9+
world
10+
5000
11+
alpha
12+
6000
13+
1987.0
14+
hello
15+
4
16+
world
17+
5000
18+
alpha
19+
6000
20+
world
21+
5000
22+
alpha
23+
6000
24+
2002
25+
hello
26+
2003

0 commit comments

Comments
 (0)