Im writing code for a library book rating system. Im currently having issues taking a populated text file, converting it into an ArrayList, and then displaying that ArrayList on a JPanel
I have a panel to create new ratings which takes inputs from the user and when the save button is pressed, it creates a new instance of the rating class and prints the rating to the file.
public Rating(int studentNum, int studentGrade, int starRating, String comments, String bookTitle) {
this.studentNum = studentNum;
this.studentGrade = studentGrade;
this.starRating = starRating;
this.comments = comments;
this.bookTitle = bookTitle;
`
try {
//PrintWriter pw = new PrintWriter(f);
PrintWriter pw = new PrintWriter(new FileWriter(f, true));
pw.println(bookTitle + " " + studentNum + " " + studentGrade + " " + starRating + " " + comments);
pw.close();
} catch (FileNotFoundException ex) {
System.out.println("File not found!");
} catch (IOException ex) {
System.out.println("An error occured when writing to the file savedRatings.txt");
}
}
This file will contain every single rating for every single book. Next, each instance of my book class contains an ArrayList of ratings which are only the ratings specific to the book being searched. The method I have to populate the ArrayList is:
public void loadRatings(String filename) {
try (BufferedReader br = new BufferedReader(new FileReader(filename))) {
String line;
while((line = br.readLine()) != null) {
String[] parts = line.split("", 5);
if(parts.length == 5 && parts[0].equals(this.title)) {
int studentNum = Integer.parseInt(parts[1]);
int studentGrade = Integer.parseInt(parts[2]);
int starRating = Integer.parseInt(parts[3]);
String comments = parts[4];
Rating rating = new Rating(studentNum, studentGrade, starRating, comments, this.title);
ratings.add(rating);
}
}
} catch (IOException ex) {
System.out.println("There was an error reading the file");
}
for(int i = 0; i < ratings.size(); i++) {
System.out.println(ratings.get(i));
}
}
Lastly, I want to be able to print these ratings into a ratings panel when searching for a book. this panel also includes information about the book. The code for that is:
private void displayRatings() {
book.loadRatings("savedRatings.txt");
`
ArrayList<Rating> ratings = book.getRatings();
for(Rating rating : ratings) {
String ratingText = "<html>Student Num: " + rating.getStudentNum() +
"<br>Student Grade: " + rating.getStudentGrade() +
"<br>Star Rating: " + rating.getStarRating() +
"<br>Comments: " + rating.getComments() + "</html>";
JLabel ratingLabel = new JLabel(ratingText);
ratingLabel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
ratingsPanel.add(ratingLabel);
}
ratingsPanel.revalidate();
ratingsPanel.repaint();
}
While the text file is being populated, im not sure if the arraylists are and when i run the code, none of the ratings show up in the ratings panel.
user25599144 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.