Using Java Write a program that reads in a student file and creates Student.java
ID: 3825690 • Letter: U
Question
Using Java 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.
Explanation / Answer
studentsdata.txt (Save this file under D Drive.Then the path of the file pointing to it is D:\studentsdata.txt t)
Williams Kane 4.5 1111
Mitchel Johnson 4.0 2222
Pat Simkox 4.5 3333
Ricky Pointing 4.0 4444
Rahul Dravid 3.5 5555
Saurav Gangully 3.0 6666
__________________
Student,java
public class Student {
//Declaring instance variables
private String firstname;
private String lastname;
private double GPA;
private int id;
//Parameterized constructor
public Student(String firstname, String lastname, double gPA, int id) {
super();
this.firstname = firstname;
this.lastname = lastname;
GPA = gPA;
this.id = id;
}
//getters and setters
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;
}
//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return "Student [firstname=" + firstname + ", lastname=" + lastname
+ ", GPA=" + GPA + ", id=" + id + "]";
}
}
___________________
package org.students;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class StudentsSearcher {
public static void main(String[] args) {
//Declaring variables
String fname,lname;
double gpa;
int id;
Student st=null;
Scanner sc=null;
//Creating an ArrayList Object which holds Students Object
ArrayList<Student> arl=new ArrayList<Student>();
//Opening an File
File inFile = new File("D:\studentsdata.txt");
try {
sc = new Scanner (inFile);
/* Reading the contents of the file and creating
* Student class object by passing values as arguments
*/
while (sc.hasNextLine())
{
fname = sc.next();
lname=sc.next();
gpa=Double.parseDouble(sc.next());
id=Integer.parseInt(sc.next());
//Creating the student class object
st=new Student(fname, lname,gpa, id);
//Adding student class obejct to the ArrayList
arl.add(st);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
finally
{
sc.close();
}
//Scanner object is used to get the inputs entered by the user
sc=new Scanner(System.in);
//Displaying all student Id's
while(true)
{
System.out.println(" Dislpaying Id's :");
for(int i=0;i<arl.size();i++)
{
System.out.println(arl.get(i).getId());
}
//getting the user entered Id for search
System.out.print(" Select Id:");
int searchId=sc.nextInt();
//Calling the method for search
int index=searchById(searchId,arl);
if(index==-1)
{
System.out.println("** Invalid Id **");
continue;
}
else
System.out.println("Student GPA :"+arl.get(index).getGPA());
//Getting the character from the user 'Y' or 'y' or 'N' or 'n'
System.out.print("Do you want to continue(Y/N) ::");
char ch = sc.next(".").charAt(0);
if(ch=='Y'||ch=='y')
continue;
else
{
System.out.println(":: Program Exit ::");
break;
}
}
}
/* This method will return the index of the
* student if the user entered valid Student Id
*/
private static int searchById(int searchId, ArrayList<Student> arl) {
for(int i=0;i<arl.size();i++)
{
if(searchId==arl.get(i).getId())
return i;
}
return -1;
}
}
___________________
Output:
Dislpaying Id's :
1111
2222
3333
4444
5555
6666
Select Id:1111
Student GPA :4.5
Do you want to continue(Y/N) ::Y
Dislpaying Id's :
1111
2222
3333
4444
5555
6666
Select Id:1224
** Invalid Id **
Dislpaying Id's :
1111
2222
3333
4444
5555
6666
Select Id:6666
Student GPA :3.0
Do you want to continue(Y/N) ::N
:: Program Exit ::
________________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.