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

Java Programming (Arrays) Write a program that reads in a student file and creat

ID: 3911374 • Letter: J

Question

Java Programming (Arrays)

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 namedStudentSearcher.java. Provide the user with a list of possible student IDs to search from (use an initialization list, for example, double[] salaries = {6.25, 6.55, 10.25, 16.85}; to populate the array, for the first and last name, GPA and student ID and PLEASE do not use a separate file like "studentsdata.txt" containing student data). 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.

There needs to be an array of students inside the StudentSearcher.java

Explanation / Answer

student_info.txt (Save this file under D Drive.Then the path of the file pointing to it is D:\students.txt )

111 Kane Williams 4.5
222 Tony Blair 4.0
333 Micky Mayer 3.0
444 Johny Mike 4.0
555 Sachin Tendulkar 4.0

___________________

Student.java

public class Student {

// Declaring instance variables
private int id;
private String firstname;
private String lastname;
private double gpa;

// Parameterized constructor
public Student(int id, String firstname, String lastname, double gpa) {
super();
this.id = id;
this.firstname = firstname;
this.lastname = lastname;
this.gpa = gpa;
}

// getters and setters
public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

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) {
this.gpa = gpa;
}

@Override
public String toString() {
return "Student [id=" + id + ", firstname=" + firstname + ", lastname=" + lastname + ", gpa=" + gpa + "]";
}


}

_______________________

StudentSearcher.java

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

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

//Declaring Student Class reference variable
Student st;

//Declaring variables
int id;
String firstname, lastname;
double gpa;
Scanner sc;
int x = 0;

//Creating an ArrayList which holds Students Objects
ArrayList < Student > arl = new ArrayList < Student > ();

//Reading the information from the file
sc = new Scanner(new File("D:\students.txt"));

/* reading the data from the file and create Student class
* objects and populate those objects into ArrayList
*/
while (sc.hasNext()) {
id = sc.nextInt();
firstname = sc.next();
lastname = sc.next();
gpa = sc.nextDouble();
//Creating Student class object
st = new Student(id, firstname, lastname, gpa);

//Adding Student object to an ArrayList
arl.add(st);
}

sc = new Scanner(System.in);

/* This while loop continues to execute
* until the user enters a sentinel
*/
int SENTINEL = -1;
while (true) {
x = 0;
System.out.print("Enter Student ID(-1 to Quit) :");
id = sc.nextInt();
if (id == SENTINEL) {
System.out.println(":: Program Exit ::");
break;
}

/* Searching the student Id in the ArrayList
* if found, display the Student Info
*/
for (Student stud: arl) {
if (stud.getId() == id) {
System.out.println(stud.toString());
x++;
}
}
if (x == 0) {
System.out.println(":: Student Id not found ::");
continue;
}
}

}

}

_________________________

Output:

Enter Student ID(-1 to Quit) :111
Student [id=111, firstname=Kane, lastname=Williams, gpa=4.5]
Enter Student ID(-1 to Quit) :555
Student [id=555, firstname=Sachin, lastname=Tendulkar, gpa=4.0]
Enter Student ID(-1 to Quit) :777
:: Student Id not found ::
Enter Student ID(-1 to Quit) :222
Student [id=222, firstname=Tony, lastname=Blair, gpa=4.0]
Enter Student ID(-1 to Quit) :-1
:: Program Exit ::

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote