Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] java – Is this simply a typo?

Posted on November 11, 2022 By

Solution 1 :

No, it’s not a typo.
BufferedReader read text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines. Then, it uses as delimiter the common System.lineSeparator() to split the text values.
Check the Javadoc by yourself.

Problem :

I’ve been reading the book Beginning Android Games and I came across this code and text:

public static void load(FileIO files) {
    BufferedReader in = null;
    try { in = new BufferedReader(new InputStreamReader(
            files.readFile(".mrnom")));
        soundEnabled = Boolean.parseBoolean( in .readLine());
        for (int i = 0; i < 5; i++) {
            highscores[i] = Integer.parseInt( in .readLine());
        }
    } catch (IOException e) {
        // :( It's ok we have defaults
    } catch (NumberFormatException e) {
        // :/ It's ok, defaults save our day
    } finally {
        try {
            if ( in != null)
                in .close();
        } catch (IOException e) {}
    }
}
public static void save(FileIO files) {
    BufferedWriter out = null;
    try {
        out = new BufferedWriter(new OutputStreamWriter(
            files.writeFile(".mrnom")));
        out.write(Boolean.toString(soundEnabled));
        for (int i = 0; i < 5; i++) {
            out.write(Integer.toString(highscores[i]));
        }
    } catch (IOException e) {} finally {
        try {
            if (out != null)
                out.close();
        } catch (IOException e) {}
    }
}

Next up is a method called save(). It takes the current settings and serializes them to
the .mrnom file on the external storage (e.g., /sdcard/.mrnom). The sound setting and each
high-score entry is stored as a separate line in that file, as expected by the load()
method. If something goes wrong, we just ignore the failure and use the default values
defined earlier. In an AAA title, you might want to inform the user about this loading
error

I am very confused as it says it writes to a new line(in the save method) so that in the load method, which uses readLine() works properly. However, they are only using write() with no /n characters. How will this work? Is it simply a typo?

READ  [FIXED] java - How to group array elements and get many occurring elements?
Powered by Inline Related Posts

Comments

Comment posted by ComputerCraft32

The delimiter is carriage return, correct? When write(string) is called, there are no spaces made from what I can tell. I tried this code in eclipse and looking at the file evertyhing is bunched together. (I change the code a bit) System.out.println’s for the boolean did not work after the save(), probably because it was the value true, along with numbers written next to it (true123). The write method just seems not to be writing the text with spaces or returns

Comment posted by Snix

Exactly, you have to modified the write method in order to make BufferedReader to understand when a piece of property ends. So, simply add a System.lineSeparator() near each value.

Comment posted by ComputerCraft32

Doesn’t that mean that the current code is typo (incorrect)?

Comment posted by Snix

The current code is a typo, the first sentence is not a typo. The BufferedReader needs the separator.

Comment posted by ComputerCraft32

Hmm okay. I find it strange that this code was able to make its way through this book. It’s even in the newer editions.

Android Tags:android, java

Post navigation

Previous Post: [FIXED] React Native floating animation
Next Post: [FIXED] java – maintain single database between customer and seller of android app

Related Posts

[FIXED] java – Removing padding and margins from Android RelativeLayout Android
[FIXED] Using Stripe payment methods with custom interface in Android Android
[FIXED] android – Flutter: MediaQuery.of(context).size is not match emulator screen size Android
[FIXED] android – why is builder in flutter breaking here Android
[FIXED] android – This app works in single activities but crahes when added to app drawer Android
[FIXED] android – How to get data from nested JSON objects using Gson Android

Archives

  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • Can Vicks humidifier be used without filter?
  • What color is Spanish green?
  • How old is Jamie in The War That Saved My Life?
  • When should I use scalp massager for hair growth?
  • Can I put polyurethane over liming wax?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme