-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudentManager.java
More file actions
78 lines (70 loc) · 2.88 KB
/
StudentManager.java
File metadata and controls
78 lines (70 loc) · 2.88 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
// StudentManager.java
import java.io.*;
import java.util.*;
public class StudentManager {
Scanner scanner = new Scanner(System.in);
String fileName = "students.txt";
public void addStudent() {
System.out.print("Enter Name: ");
String name = scanner.nextLine();
System.out.print("Enter Roll Number: ");
String roll = scanner.nextLine();
System.out.print("Enter Department: ");
String dept = scanner.nextLine();
System.out.print("Enter Email: ");
String email = scanner.nextLine();
try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileName, true))) {
writer.write(name + "," + roll + "," + dept + "," + email);
writer.newLine();
System.out.println("✅ Student added and saved to file!");
} catch (IOException e) {
System.out.println("❌ Error saving student: " + e.getMessage());
}
}
public void viewAll() {
File file = new File(fileName);
if (!file.exists() || file.length() == 0) {
System.out.println("❗ No student records found.");
return;
}
System.out.println("\n🎓 All Student Records:");
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
String line;
while ((line = reader.readLine()) != null) {
String[] data = line.split(",");
System.out.println("Name: " + data[0]);
System.out.println("Roll No: " + data[1]);
System.out.println("Department: " + data[2]);
System.out.println("Email: " + data[3]);
System.out.println("-----------------------------");
}
} catch (IOException e) {
System.out.println("❌ Error reading file: " + e.getMessage());
}
}
public void searchStudent() {
System.out.print("Enter Roll Number to search: ");
String roll = scanner.nextLine();
boolean found = false;
try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) {
String line;
while ((line = reader.readLine()) != null) {
String[] data = line.split(",");
if (data[1].equalsIgnoreCase(roll)) {
System.out.println("\n🎯 Student Found:");
System.out.println("Name: " + data[0]);
System.out.println("Roll No: " + data[1]);
System.out.println("Department: " + data[2]);
System.out.println("Email: " + data[3]);
found = true;
break;
}
}
} catch (IOException e) {
System.out.println("❌ Error reading file: " + e.getMessage());
}
if (!found) {
System.out.println("❌ Student not found.");
}
}
}