Question: Java (Constructor issue) Trying to figure out how to read in a CSV file into a constructor. Theme is harry potter and I'm reading in
Java (Constructor issue) Trying to figure out how to read in a CSV file into a constructor. Theme is harry potter and I'm reading in 4 files (Gryffindor, Hufflepuff, Ravenclaw, Slytherin). Reading this into my House.java, but School.java and Student.java have constructors/getters and setters as well. Supplied below. House.java
private String name;
private String color;
private String professor;
public ArrayList Students;
public House(String name, String color, String professor, ArrayList stdList) {
this.name = name;
this.color = color;
this.professor = professor;
this.Students = stdList;
}
public void dataLoader() {
String fileName = "Gryffindor.csv";
String content = null;
/*
* Shows working path System.out.println(newFile(".").getAbsoluteFile());
*/
try {
Scanner inputStream = new Scanner(new File(fileName));
// Delimits by commas and (enter or )
inputStream.useDelimiter("[, ]");
// Iteration rather than iterable
while (inputStream.hasNext()) {
content = inputStream.nextLine();
}
inputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
} School.java
public class School {
private String name;
private int enrollment;
private House houses[];
/*
* School Constructor
*
* @param pass in name to set name
*/
public School(String name) {
this.name = name;
houses = new House[4];
enrollment = 0;
} Student.java
public class Student {
private String firstName;
private String lastName;
private int year;
public Student(String firstName, String lastName, int year) {
this.firstName = firstName;
this.lastName = lastName;
this.year = year;
} Trying to be discrete about the entire program since I wrote it and don't want it to be tracked as plagerism. House.java - how do I read into this CSV file into a constructor. Comma delmited and needs to be read in and not manually entered. *Example of Gryffindor.csv*
Colin Creevy,1, Hermione Granger,2, Harry Potter 2,
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
