Java Projects Students
Java Projects Students
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(){
}
//stu.createFile();
}