It's probably time to ask for help. Below is the code I've written. I keep getting the 2nd requirement wrong, and the tip from the validator is, "Check why the users list is empty after the file has been read". I understand that the problem must be with the load method that doesn't do what I'm making it do. But I have no clue why. For some reason it reads a null string when it is expected to read the number of users (users.size()), which I send to printWriter in the save method. And maybe something else is wrong. I'm kind of stuck.
Here is the screen output I receive when I run the program:
The number of users is 1
Saving: Ginny, Weasley, Mon Jan 10 00:00:00 CET 1983, false, United Kingdom
Exception: Cannot parse null string
false
Ginny
Weasley
1983-01-10
female
UNITED_KINGDOM
java.lang.NumberFormatException: Cannot parse null string
at java.base/java.lang.Integer.parseInt(Integer.java:630)
at java.base/java.lang.Integer.parseInt(Integer.java:786)
at com.codegym.task.task20.task2002.Solution$CodeGym.load(Solution.java:99)
at com.codegym.task.task20.task2002.Solution.main(Solution.java:42)
Process finished with exit code 0
package com.codegym.task.task20.task2002;
import java.io.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/*
Reading and writing to a file: CodeGym
*/
public class Solution {
public static void main(String[] args) {
// You can find your_file_name.tmp in your TMP directory or adjust outputStream/inputStream according to your file's actual location
try {
//File yourFile = File.createTempFile("tempfile", ".tmp");
File yourFile = new File("C:\\Users\\passe\\Documents\\Human assets.txt");
OutputStream outputStream = new FileOutputStream(yourFile);
//we don't open the inputStream now; we'll open it AFTER closing the outputStream
CodeGym codeGym = new CodeGym();
// Initialize users field for the codeGym object here
//e.g. lets's add Ginny Weasley
User user = new User();
user.setFirstName("Ginny"); user.setLastName("Weasley");
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
user.setBirthDate(dateFormat.parse("1983-01-10"));
user.setMale(false);
user.setCountry(User.Country.UNITED_KINGDOM);
codeGym.users.add(user);
codeGym.save(outputStream);
outputStream.flush();
outputStream.close();
InputStream inputStream = new FileInputStream(yourFile);
CodeGym loadedObject = new CodeGym();
loadedObject.load(inputStream);
// Here check that the codeGym object is equal to the loadedObject object
System.out.println(codeGym.equals(loadedObject));
//testing snippet
for (User u : codeGym.users) {
System.out.println(u.getFirstName());
System.out.println(u.getLastName());
String stringBD = dateFormat.format(u.getBirthDate());
System.out.println(stringBD);
if (u.isMale() == true) System.out.println("male"); else System.out.println("female");
System.out.println(u.getCountry());
}
inputStream.close();
} catch (IOException e) {
//e.printStackTrace();
System.out.println("Oops, something is wrong with my file");
} catch (Exception e) {
//e.printStackTrace();
System.out.println("Oops, something is wrong with the save/load method");
}
}
public static class CodeGym {
public List<User> users = new ArrayList<>();
public void save(OutputStream outputStream) throws Exception {
// Implement this method
PrintWriter printWriter = new PrintWriter(outputStream);
printWriter.println(users.size()); //we check the number of users first
//debugging
System.out.println("The number of users is " + users.size());
if (!this.users.isEmpty()) {
for (User user : this.users) {
printWriter.println(user.getFirstName());
printWriter.println(user.getLastName());
Date birthDate = user.getBirthDate();
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String stringBD = dateFormat.format(birthDate);
printWriter.println(stringBD);
if (user.isMale() == true) printWriter.println("male");
else printWriter.println("female");
printWriter.println(user.getCountry().getDisplayName());
//debugging
System.out.println("Saving: " + user.getFirstName() + ", " + user.getLastName() + ", " + birthDate + ", " + user.isMale() + ", " + user.getCountry().getDisplayName());
}
}
}
public void load(InputStream inputStream) throws Exception {
// Implement this method
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
try {
//int userCount = Integer.parseInt(reader.readLine()); for (int i = 0; i < userCount; i++)
int userCount = Integer.parseInt(reader.readLine());
//debugging
System.out.println("The number of users is still or not any longer " + userCount);
for (int i = 0; i < userCount; i++) {
String firstName = reader.readLine();
String lastName = reader.readLine();
Date birthDate = dateFormat.parse(reader.readLine());
String gender = reader.readLine();
String country = reader.readLine();
// Debugging
System.out.println("Loaded: " + firstName + ", " + lastName + ", " + birthDate + ", " + gender + ", " + country);
User user = new User();
user.setFirstName(firstName);
user.setLastName(lastName);
user.setBirthDate(birthDate);
user.setMale(gender.equals("male"));
switch (country) {
case "United States":
user.setCountry(User.Country.UNITED_STATES);
break;
case "United Kingdom":
user.setCountry(User.Country.UNITED_KINGDOM);
break;
default:
user.setCountry(User.Country.OTHER);
}
this.users.add(user);
// Debugging
//System.out.println("Loaded: " + user.getFirstName() + ", " + user.getLastName() + ", " + birthDate + ", " + user.isMale() + ", " + user.getCountry().getDisplayName());
}
} catch (Exception e) {
System.out.println("Exception: " + e.getMessage());
e.printStackTrace(); // Additional debugging
}
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
CodeGym codeGym = (CodeGym) o;
return users != null ? users.equals(codeGym.users) : codeGym.users == null;
}
@Override
public int hashCode() {
return users != null ? users.hashCode() : 0;
}
}
}