import java.nio.file.*;
import java.io.*;
import java.util.Scanner;
public class SearchStudentFiles
{
public static void main(String[] args)
{
String studentSearch ="";
while (!"999".equals(studentSearch))
{
File file = new File("/Users/Graham/NetBeansProjects/StudentSort/HonorsStudentList.txt"); //PATH WILL BE DIFFERENT
File file2 = new File("/Users/Graham/NetBeansProjects/StudentSort/GoodStandingStudentList.txt"); //PATH WILL BE DIFFERENT
File file3 = new File("/Users/Graham/NetBeansProjects/StudentSort/ProbationStudentList.txt"); //PATH WILL BE DIFFERENT
Scanner scan = new Scanner(System.in);
System.out.println("Enter student's First name, Last name, or Student ID Number to SEARCH: ");
studentSearch = scan.next();
Scanner scanner;
//Honors Students File Search
try
{
scanner = new Scanner(file).useDelimiter(",");
boolean found = false;
while (scanner.hasNext())
{
final String lineFromFile = scanner.nextLine();
if (lineFromFile.contains(studentSearch))
{
Student student = new Student();
String[] fieldArray = new String[3];
String delimiter = ",";
fieldArray = lineFromFile.split(delimiter);
student.setStudentId(Integer.parseInt(fieldArray[0]));
student.setLastName(fieldArray[1]);
student.setFirstName(fieldArray[2]);
student.setGradePoint(Double.parseDouble(fieldArray[3]));
System.out.printf("%-15s%-15s%13s\n", "Student ID", "Name (First Last)", "GPA");
System.out.printf("%-15s%-12s%-10s%8.1f%25s\n",
student.getStudentId(),
student.getFirstName(),
student.getLastName(),
student.getGradePoint(),
"Honor Student");
found = true;
break;
}
if(!found){
System.out.println("Student not found. Please try again.");
}
}
}
catch (IOException e)
{
System.out.println(" SOMETHING WENT WRONG " + file.toString());
}
//Students in Good Standing File Search
try
{
scanner = new Scanner(file2).useDelimiter(",");
boolean found = false;
while (scanner.hasNext())
{
final String lineFromFile = scanner.nextLine();
if (lineFromFile.contains(studentSearch))
{
Student student = new Student();
String[] fieldArray = new String[3];
String delimiter = ",";
fieldArray = lineFromFile.split(delimiter);
student.setStudentId(Integer.parseInt(fieldArray[0]));
student.setLastName(fieldArray[1]);
student.setFirstName(fieldArray[2]);
student.setGradePoint(Double.parseDouble(fieldArray[3]));
System.out.printf("%-15s%-15s%13s\n", "Student ID", "Name (First Last)", "GPA");
System.out.printf("%-15s%-12s%-10s%8.1f%25s\n",
student.getStudentId(),
student.getFirstName(),
student.getLastName(),
student.getGradePoint(),
"Good Standing Student");
found = true;
break;
}
if(!found){
System.out.println("Student not found. Please try again.");
}
}
}
catch (IOException e)
{
System.out.println(" SOMETHING WENT WRONG " + file.toString());
}
//Probation Student File Search
try
{
scanner = new Scanner(file3).useDelimiter(",");
boolean found = false;
while (scanner.hasNext())
{
final String lineFromFile = scanner.nextLine();
if (lineFromFile.contains(studentSearch))
{
Student student = new Student();
String[] fieldArray = new String[3];
String delimiter = ",";
fieldArray = lineFromFile.split(delimiter);
student.setStudentId(Integer.parseInt(fieldArray[0]));
student.setLastName(fieldArray[1]);
student.setFirstName(fieldArray[2]);
student.setGradePoint(Double.parseDouble(fieldArray[3]));
System.out.printf("%-15s%-15s%13s\n", "Student ID", "Name (First Last)", "GPA");
System.out.printf("%-15s%-12s%-10s%8.1f%25s\n",
student.getStudentId(),
student.getFirstName(),
student.getLastName(),
student.getGradePoint(),
"Probation Student");
found = true;
break;
}
if(!found){
System.out.println("Student not found. Please try again.");
}
}
}
catch (IOException e)
{
System.out.println(" SOMETHING WENT WRONG " + file.toString());
}
}
}
}