How do I read students from a text file. A student can have multiple subjects. I want to store student data from file and display the data stored using main.
THIS IS WHAT I HAVE SO FAR.
public class Student {
private String name;
private int id;
private boolean susendedStatus;
private Subject subjects;
/**
* Get the value of subjects
*
* @return the value of subjects
*/
public Subject getSubjects() {
return subjects;
}
/**
* Set the value of subjects
*
* @param subjects new value of subjects
*/
public void setSubjects(Subject subjects) {
this.subjects = subjects;
}
/**
* Get the value of susendedStatus
*
* @return the value of susendedStatus
*/
public boolean isSusendedStatus() {
return susendedStatus;
}
/**
* Set the value of susendedStatus
*
* @param susendedStatus new value of susendedStatus
*/
public void setSusendedStatus(boolean susendedStatus) {
this.susendedStatus = susendedStatus;
}
/**
* Get the value of id
*
* @return the value of id
*/
public int getId() {
return id;
}
/**
* Set the value of id
*
* @param id new value of id
*/
public void setId(int id) {
this.id = id;
}
/**
* Get the value of name
*
* @return the value of name
*/
public String getName() {
return name;
}
/**
* Set the value of name
*
* @param name new value of name
*/
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return “Student{” + “name=” + name + “, id=” + id + “, susendedStatus=” + susendedStatus + “, subjects=” + subjects + ‘}’;
}
}
students.txt
David Smith,111,true,Basket Weaving,3,Programming,6,Maths,4
Andy Cheng,333,true,Programming,6,Operating Systems,5,Maths,4
Susan Dally,123,false
Zak Whatever,199,true,Rocket Science,3,Social Media,3,English,4,Basket Weaving,3
Russell Crowe,222,true,Maths,4
John Citizen,777,true
Jane Citizen,555,true,Social Media,3,Rocket Science,3,Systems Design,5
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
/**
*
* @author HP
*/
public class StudentDatabase {
ArrayList studentList;
public StudentDatabase() {
this.studentList = new ArrayList<>();
loadStudents();
}
public void loadStudents() {
//Creating Scanner instance to read File in Java
Scanner scnr = null;
Student student = null;
try {
//creating File instance to reference text file in Java
File text = new File(“subjects.txt”);
scnr = new Scanner(text);
//Reading each line of the file using Scanner class
int lineNumber = 1;
while (scnr.hasNextLine()) {
String line = scnr.nextLine();
String[] students = line.split(” “);
System.out.println(“line ” + lineNumber + ” :” + line);
lineNumber++;
student = new Student();
studentList.add(student);
}
} catch (FileNotFoundException ex) {
System.out.println(“Error : ” + ex.getMessage());
}
if (scnr != null) {
scnr.close();
}
}
}
SUBMITE THE UPDATED CODE AND SCREENSHOTS OF THE PROGRAM IN ACTION.
ALSO DISPLAY THE DATA IN IN MAIN CLASS
public class Main{
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
StudentDatabase studentDatabase = new StudentDatabase();
//Loop through StudentDatabase and display data from text file when the program starts.
}
}
public class Subject {
private String name;
private int creditPoints;
public Subject() {
}
/**
*
* @param name
* @param creditPoints
*/
public Subject(String name, int creditPoints) {
this.name = name;
this.creditPoints = creditPoints;
}
/**
* Get the value of creditPoints
*
* @return the value of creditPoints
*/
public int getCreditPoints() {
return creditPoints;
}
/**
* Set the value of creditPoints
*
* @param creditPoints new value of creditPoints
*/
public void setCreditPoints(int creditPoints) {
this.creditPoints = creditPoints;
}
/**
* Get the value of name
*
* @return the value of name
*/
public String getName() {
return name;
}
/**
* Set the value of name
*
* @param name new value of name
*/
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return “Subject{” + “name=” + name + “, creditPoints=” + creditPoints + ‘}’;
}
}
Delivering a high-quality product at a reasonable price is not enough anymore.
That’s why we have developed 5 beneficial guarantees that will make your experience with our service enjoyable, easy, and safe.
You have to be 100% sure of the quality of your product to give a money-back guarantee. This describes us perfectly. Make sure that this guarantee is totally transparent.
Read moreEach paper is composed from scratch, according to your instructions. It is then checked by our plagiarism-detection software. There is no gap where plagiarism could squeeze in.
Read moreThanks to our free revisions, there is no way for you to be unsatisfied. We will work on your paper until you are completely happy with the result.
Read moreYour email is safe, as we store it according to international data protection rules. Your bank details are secure, as we use only reliable payment systems.
Read moreBy sending us your money, you buy the service we provide. Check out our terms and conditions if you prefer business talks to be laid out in official language.
Read more