Skip to content

Commit f90c61f

Browse files
committed
Create exception.java
1 parent 9d2b21c commit f90c61f

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

Exceptions/exception.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Create a class as Student. Write a program in Java to display the names and
3+
* roll numbers of students. Create an array of 10 students and initialize the
4+
* array with user input. Handle ArrayIndexOutOfBoundsException, so that any
5+
* such problem doesn’t cause illegal termination of program. Read a character
6+
* from user and display the student names starting with given character.
7+
*/
8+
9+
package Exceptions;
10+
11+
import java.util.Scanner;
12+
13+
class Student {
14+
String name;
15+
int rno;
16+
17+
Student(String name, int rno) {
18+
this.name = name;
19+
this.rno = rno;
20+
}
21+
22+
void display() {
23+
System.out.println("Name: " + name + "\nRoll Number: " + rno);
24+
}
25+
}
26+
27+
public class exception {
28+
public static void amin(String[] args){
29+
String name;
30+
int rno;
31+
Scanner s1 = new Scanner(System.in);
32+
Scanner s2 = new Scanner(System.in);
33+
34+
Student st[] = new Student[5];
35+
try {
36+
for (int i = 0; i < 5; i++) {
37+
System.out.println("Enter the name: ");
38+
name = s1.nextLine();
39+
System.out.println("Enter the roll number: ");
40+
rno = s2.nextInt();
41+
st[i] = new Student(name, rno);
42+
}
43+
} catch (ArrayIndexOutOfBoundsException e) {
44+
e.printStackTrace();
45+
}
46+
47+
System.out.println("Enter the first character: ");
48+
char check = s1.next().charAt(0);
49+
try {
50+
for (int i = 0; i < 5; i++) {
51+
if (check == st[i].name.charAt(0)) {
52+
st[i].display();
53+
}
54+
}
55+
} catch (ArrayIndexOutOfBoundsException e) {
56+
e.printStackTrace();
57+
}
58+
59+
s1.close();
60+
s2.close();
61+
}
62+
}

0 commit comments

Comments
 (0)