1

Whatever I did, I did not write a new data on a new line in the file. How can I fix it?

For example mary's score is 100 and smith's score is 150, but in the txt file it is

 mary 100smith 150

I wanna smith 150 in a new line

public class HighScores {
public HighScores(){

    String txt = "";
    Scanner sc = null;
    PrintWriter pw = null;
    File Checker = null;
    try{
        Checker = new File("highScores.txt");
        if(!Checker.exists()){
            Checker.createNewFile();
        }


sc = new Scanner(new File("highScores.txt"));
        while(sc.hasNextLine()){
            txt = txt.concat(sc.nextLine()+"\n");
        }

        String score=String.valueOf(Game3.score);
        String name = NewPlayer.name;

        txt = txt.concat(name + " "+ score +"\n");
        pw = new PrintWriter(Checker);// writing the checker
        pw.write(txt +"\r\n");

pw.println() gives the same problem too.

    }catch(FileNotFoundException fnfe){

        fnfe.printStackTrace();
    }catch(IOException ioe){
        ioe.printStackTrace();
    }finally{
        sc.close();
        pw.close();
    }

}

}
RubioRic
  • 2,442
  • 4
  • 28
  • 35
crescent
  • 61
  • 4
  • 1
    Don't use concat() and hard-code `\n`or `\r\n`. Open your PrintWriter, and use println() to write some text followed by an EOL, or print() to write some text not followed by an EOL. And respect the Java naming conventions: variables start with a lowercase letter. – JB Nizet Apr 09 '16 at 07:25
  • I used println, But again the second data was not in a new line – crescent Apr 09 '16 at 07:27
  • There is no way to have changed all your code and written this new comment in less than 1 minute. Re-read my comment again. – JB Nizet Apr 09 '16 at 07:29
  • 1
    See this thread, make the things simple, and it will easier and lasts for your, How to write new line character to a file in Java http://stackoverflow.com/questions/19084352/how-to-write-new-line-character-to-a-file-in-java – ArifMustafa Apr 09 '16 at 07:31
  • `println()` does *not* give the same problem. It provides the solution. – user207421 Apr 09 '16 at 08:30

2 Answers2

0

The code is dodgy, but should actually do what you expect of it.

You are probably viewing the resulting file with a Windows notepad app. It expects "\r\n" as a newline separator, as do most other Windows apps.

  • Nope I am viewing the resulting file with JCreator. – crescent Apr 09 '16 at 07:31
  • That can still be the cause =) Try outputting `txt` to `System.out` to see if the newlines are actually written and if they are. try changing the line separators to `\r\n`. – ThatDamnRusski Apr 09 '16 at 07:43
0

Use the following code

public class HighScores {
public HighScores() {
    String txt = "";
    Scanner sc = null;
    PrintWriter pw = null;
    File Checker = null;
    try {
        Checker = new File("highScores.txt");
        if (!Checker.exists()) {
            Checker.createNewFile();
        }

        sc = new Scanner(new File("highScores.txt"));
        while (sc.hasNextLine()) {
            txt = txt.concat(sc.nextLine() + "\n");
        }

        String score = String.valueOf(Game3.score);
        String name = NewPlayer.name;

        txt = txt + name + " " + score;
        pw = new PrintWriter(Checker);// writing the checker

        pw.println(txt);
    } catch (FileNotFoundException fnfe) {

        fnfe.printStackTrace();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    } finally {
        sc.close();
        pw.close();
    }
}

}

Geeth Lochana
  • 164
  • 13
  • The `exists()/createNewFile()` part is pointless, as usual, and mere code is not an answer here. You have to explain, – user207421 Apr 09 '16 at 10:01