File tree Expand file tree Collapse file tree 1 file changed +62
-0
lines changed
Expand file tree Collapse file tree 1 file changed +62
-0
lines changed Original file line number Diff line number Diff line change 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 + "\n Roll 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+ }
You can’t perform that action at this time.
0 commit comments