Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

in jgrasp Write a program that reads in a student file and creates Student.java

ID: 3739417 • Letter: I

Question

in jgrasp Write a program that reads in a student file and creates Student.java objects with the following fields: first name, last name, GPA, and student ID. The objects will be stored in an array of Students in a class named StudentSearcher.java. Provide the user with a list of possible student IDs to search from. When a user enters in a student ID, the program will search the array and display the found student's GPA. The program will continue to search students until the user exits the program.

I'm having difficulty pulling up the student results and cant figure how to make the array, please help

Explanation / Answer

StudentSearcher.java

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Scanner;

public class StudentSearcher {

public static void main(String[] args) throws FileNotFoundException {

Scanner scan = new Scanner(System.in);

System.out.println("Enter the student array size: ");

int size = scan.nextInt();

Student s[] = new Student[size];

String fileName = "D:\studentsdata.txt";

File file = new File(fileName);

Scanner read = new Scanner(file);

int lineCount = 0;

while(read.hasNextLine()) {

int id = read.nextInt();

String firstName = read.next();

String lastName = read.next();

double gpa = read.nextDouble();

s[lineCount++] = new Student(id, firstName, lastName, gpa);

}

System.out.println("Student data loaded successfully");

System.out.println("Enter the search student ID(-1 to quit): ");

int searchId = scan.nextInt();

while(searchId != -1) {

Student matchStudent = getSearchStudent(s, searchId);

if(matchStudent != null) {

System.out.println("Student fOund with ID "+searchId);

System.out.println("Student GPA: "+matchStudent.getGPA());

} else {

System.out.println("Student not found with ID "+searchId);

}

System.out.println("Enter the search student ID(-1 to quit): ");

searchId = scan.nextInt();

}

read.close();

scan.close();

}

public static Student getSearchStudent(Student s[], int id) {

for(int i=0;i<s.length; i++) {

if(s[i].getId() == id) {

return s[i];

}

}

return null;

}

}

Student.java

public class Student {

private String firstName, lastName;

private double GPA;

private int id;

public Student(int id, String firstName, String lastName, double GPA) {

this.firstName=firstName;

this.lastName = lastName;

this.id = id;

this.GPA = GPA;

}

public String getFirstName() {

return firstName;

}

public void setFirstName(String firstName) {

this.firstName = firstName;

}

public String getLastName() {

return lastName;

}

public void setLastName(String lastName) {

this.lastName = lastName;

}

public double getGPA() {

return GPA;

}

public void setGPA(double gPA) {

GPA = gPA;

}

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

}

Output:-

Enter the student array size:
5
Student data loaded successfully
Enter the search student ID(-1 to quit):
333
Student fOund with ID 333
Student GPA: 77.7
Enter the search student ID(-1 to quit):
66
Student not found with ID 66
Enter the search student ID(-1 to quit):
111
Student fOund with ID 111
Student GPA: 88.8
Enter the search student ID(-1 to quit):
-1

studentsdata.txt:

111 Suresh Murapaka 88.8
222 Sekhar Murapaka 99.9
333 Anshu M 77.7
444 Revathi K 66.6
555 Narayana M 55.5