0% found this document useful (0 votes)
170 views

Java Projects Students

This document contains Java code for a Student class that can create, read from, and write to a student.txt file. The Student class contains methods to create the file if it doesn't exist, add a new student by prompting the user for input and writing to the file, display all students by reading from the file line by line, and search for a student by ID by reading the file and comparing IDs. The TestStudent class contains a main method that displays a menu allowing the user to choose to add, search for, display, or quit, and calls the appropriate Student class method.

Uploaded by

Fetiya Kedir
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
170 views

Java Projects Students

This document contains Java code for a Student class that can create, read from, and write to a student.txt file. The Student class contains methods to create the file if it doesn't exist, add a new student by prompting the user for input and writing to the file, display all students by reading from the file line by line, and search for a student by ID by reading the file and comparing IDs. The TestStudent class contains a main method that displays a menu allowing the user to choose to add, search for, display, or quit, and calls the appropriate Student class method.

Uploaded by

Fetiya Kedir
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Saved on the project class

package student;
import java.io.*;
import java.util.Scanner;
public class Student {
File studFile=new File("student.txt");
public void createFile(){
try{
if(studFile.createNewFile()){
System.out.println("File Created successfully!");
}
}
catch(IOException ex){
}
}
public void addStudent(){
Scanner input=new Scanner(System.in);
System.out.print("Enter Student ID:"+" ");
String studentid=input.nextLine();//input.next
System.out.print("Enter Student's First Name:"+" ");
String firstName=input.nextLine();
System.out.print("Enter Student's Last Name:"+" ");
String lastName=input.nextLine();
try{
FileWriter fw=new FileWriter(studFile,true);
fw.write(id+" "+firstName+" "+lastName+" "+"\n");
fw.close();
}
catch(IOException ex){
System.out.println(ex.getMessage());
}
}
public void displayStudent(){
try{
Scanner input= new Scanner(studFile);
while(input.hasNext()){
String line=input.nextLine();
System.out.println(line);
}
}
catch(FileNotFoundException ex){
System.out.println("Cannot read!"+ex.getMessage());
}
}
public void searchStudent(){
System.out.println("Enter Student ID Number to search.....");
try{
Scanner in=new Scanner(System.in);
String id=in.next();
while(in.hasNext()){
String line =in.nextLine();
Scanner search = new Scanner (line);
String searchedId=search.next();

if(id.equals(searchedId)){

System.out.println(line);
}
}
}
catch(IOException ex){
System.out.println("Cannot read!"+ex.getMessage());
}

}
Saved on the project subclass
package student;
import java.util.Scanner;
public class TestStudent {
public static void main(String[] args) {
mainMenu();
}
public static void mainMenu(){

Student stu=new Student();


stu.createFile();
System.out.println("********Main Menu********");
System.out.println("Press 1 to Add Student");
System.out.println("Press 2 to Search Student");
System.out.println("Press 3 to Display Student");
System.out.println("Press 0 to Quit the Program \n \n");

System.out.println("Enter Your Choice...");


Scanner input=new Scanner(System.in);
String myChoice=input.next();
if("1".equals(myChoice)){
stu.addStudent();
mainMenu();//Back to main Menu
}
else if("2".equals(myChoice)){
stu.searchStudent();
mainMenu();
}
else if("3".equals(myChoice)){
stu.displayStudent();
mainMenu();//Back to main menu
}
else if("0".equals(myChoice)){
System.out.println("System Terminated!!!");

}
//stu.createFile();
}

You might also like