CodeGym
Promotion
CodeGym University
Learning
Course
Tasks
Surveys & Quizzes
Games
Help
Schedule
Community
Users
Forum
Chat
Articles
Success stories
Activity
Reviews
Subscriptions
Light theme
Start learning now
  • All questions
Ivan
Level 22
Nope
  • 06.04.2020
  • 686views
  • 3comments

I passed but rewrote my code. Where is the issue in my first code?

Question about the task Reading and writing to a file: CodeGym
Java Core,  Level 10,  Lesson 2
Under discussion
0
Comments (3)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
syan
Level 20 , United States
14 August 2020, 18:04
public void save(OutputStream outputStream) throws Exception { PrintWriter pw = new PrintWriter(outputStream); for (User user : users) { pw.write(user.getFirstName() + "," + user.getLastName() + "," + user.getBirthDate()+ "," + user.isMale() + "," + user.getCountry().getDisplayName() + "\n"); } pw.flush(); } public void load(InputStream inputStream) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(inputStream)); String str; String[] temp; while ((str = br.readLine()) != null) { temp = str.split(","); User user = new User(); user.setFirstName(temp[0]); user.setLastName(temp[1]); user.setBirthDate(new Date(temp[2])); switch (temp[3]) { case "true": user.setMale(true); break; case "false": user.setMale(false); break; } switch (temp[4]) { case "United States": user.setCountry(User.Country.UNITED_STATES); break; case "United Kingdom": user.setCountry(User.Country.UNITED_KINGDOM); break; case "Other": user.setCountry(User.Country.OTHER); break; } users.add(user); } }
0
Nouser
Level 36 , Germany
4 May 2020, 12:46
I like the solution, guess I rewrite mine so it is working like yours. I saved the list size as first line into the file. Problems I've seen in your solution that may stop it from working 1. infinite loop in the save method - while (!(users.isEmpty())){ 2. it's far easier to save the birth date as long 3. in the load method... there are no users in the list, so it'll never load while (!(users.isEmpty())){ Just a hint, if you want to get rid of all the comparisons..
boolean isMale = Boolean.parseBoolean(reader.readLine());
User.Country country = User.Country.valueOf(reader.readLine());
0
Nouser
Level 36 , Germany
4 May 2020, 12:58
OK, have changed my methods, too, thanks ;)
public void save(OutputStream outputStream) throws Exception {
    PrintWriter writer = new PrintWriter(outputStream);
    if (!users.isEmpty()) {
        for (User u : users) {
            writer.println(u.getFirstName());
            writer.println(u.getLastName());
            writer.println(u.getBirthDate().getTime());
            writer.println(u.isMale());
            writer.println(u.getCountry());
        }
    }
    writer.flush();
}

public void load(InputStream inputStream) throws Exception {
    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
    while (reader.ready()) {
            //I've added a constructor (firstName, lastName, birthDate, isMale, coutry)
            users.add( new User(reader.readLine(), /* firstName*/
                                reader.readLine(), /* lastName */
                                new Date(Long.parseLong(reader.readLine())), /* birthDate */
                                Boolean.parseBoolean(reader.readLine()), /* isMale */
                                User.Country.valueOf(reader.readLine()) /* country */
                                ));
    }
}
0
Learn
  • Registration
  • Java Course
  • Help with Tasks
  • Pricing
  • Game Projects
  • Java Syntax
Community
  • Users
  • Articles
  • Forum
  • Chat
  • Success Stories
  • Activity
  • Affiliate Program
Company
  • About us
  • Contacts
  • Reviews
  • Press Room
  • CodeGym for EDU
  • FAQ
  • Support
CodeGym CodeGym is an online course for learning Java programming from scratch. This course is a perfect way to master Java for beginners. It contains 1200+ tasks with instant verification and an essential scope of Java fundamentals theory. To help you succeed in education, we’ve implemented a set of motivational features: quizzes, coding projects, content about efficient learning and Java developer’s career.
Follow us
Interface language
Programmers Are Made, Not Born © 2023 CodeGym
MastercardVisa
Programmers Are Made, Not Born © 2023 CodeGym
This website uses cookies to provide you with personalized service. By using this website, you agree to our use of cookies. If you require more details, please read our Terms and Policy.