Results 1 to 13 of 13
  1. #1
    tcms is offline Member
    Join Date
    May 2013
    Posts
    43
    Rep Power
    0

    Default Update .txt File with Highscore

    Hello ,

    I made a method that creates a txt File with Scores, the problem is that I don't want to create a new file everytime I have a new Score. What I want is: if the file have at least one line (one score) update the file, inserting the new Score. The code that I have is this:

    Java Code:
    public void save_to_txt(String save_file, Player a) {
    
    		try {
    		
    				PrintWriter fileWriter = new PrintWriter(new PrintWriter(
    						save_file));
    
    				fileWriter.println(a.getName() + " - " + a.getScore());
    				}
    				fileWriter.close();
    			}
    		} catch (final FileNotFoundException exception) {
    			System.out
    					.println("Saving Error. Try again later!");
    		}
    	}
    Last edited by tcms; 05-23-2013 at 03:00 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default Re: Update .txt File with Highscore

    Take a look at the API. You'll notice that the PrintWriter constructor you're using empties the file when it is opened. You probably don't want to do that.

    Do you want to append to a file? Or do you want to keep track of a certain number of high scores?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    tcms is offline Member
    Join Date
    May 2013
    Posts
    43
    Rep Power
    0

    Default Re: Update .txt File with Highscore

    Quote Originally Posted by KevinWorkman View Post
    Take a look at the API. You'll notice that the PrintWriter constructor you're using empties the file when it is opened. You probably don't want to do that.

    Do you want to append to a file? Or do you want to keep track of a certain number of high scores?
    If the file isn't empty I want to add a new Score, so I want to append. Later I will use a compare to Sort the scores on the file

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default Re: Update .txt File with Highscore

    Quote Originally Posted by tcms View Post
    If the file isn't empty I want to add a new Score, so I want to append. Later I will use a compare to Sort the scores on the file
    I'd start with a google search of "java append to file". It's only a slight change to your existing code.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    tcms is offline Member
    Join Date
    May 2013
    Posts
    43
    Rep Power
    0

    Default Re: Update .txt File with Highscore

    Quote Originally Posted by KevinWorkman View Post
    I'd start with a google search of "java append to file". It's only a slight change to your existing code.
    try {

    I made it like this

    Java Code:
    	File file = new File(save_file);
    			if (file.exists()) {
    
    				PrintWriter a = new PrintWriter(new FileOutputStream(save_file,true));
    				a.append("test--test--test");
    But it doesnt work.

  6. #6
    tcms is offline Member
    Join Date
    May 2013
    Posts
    43
    Rep Power
    0

    Default Re: Update .txt File with Highscore

    I made it with FileWriter! It works with the append. I only had to put true in the second parameter. (LOL)

    Now I have another question, it's possible to sort this txt file? Because the only way that I have to collect/save the score is when the game ends.

    If i have a txt file like this:

    Player A - 10 POINTS
    Player B - 15 POINTS
    Player C - 5 POINTS
    My ideia is create a scanner , read the final file (with the last score added), sort it, and make a new txt file with the right order.

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Update .txt File with Highscore

    If you want the data in the text file to be sorted then you will need to read it all, sort it and then write the data back to the file. you may find it easier for your program to do that anyway.
    Program starts
    Read all data from text file
    Add/delete data
    Maintain a sorted list
    On exit write data back to text file.

  8. #8
    tcms is offline Member
    Join Date
    May 2013
    Posts
    43
    Rep Power
    0

    Default Re: Update .txt File with Highscore

    Quote Originally Posted by Junky View Post
    If you want the data in the text file to be sorted then you will need to read it all, sort it and then write the data back to the file. you may find it easier for your program to do that anyway.
    Program starts
    Read all data from text file
    Add/delete data
    Maintain a sorted list
    On exit write data back to text file.
    That was what i thought, seems a good idrea. now i'am getting troubles with load of the file, to put the stored Scores in an Arraylist.

    I have this:

    Java Code:
    public ArrayList<HighScores> load_from_txt(String save_file) {
    		ArrayList<HighScores> temp = new ArrayList<HighScores>();
    
    		Scanner scanner = new Scanner(save_file);
    		while (scanner.hasNextLine()) {
    			String linha = scanner.nextLine();
    			Scanner linhaScanner = new Scanner(linha);
    
    			String nome = linhaScanner.next();
    			int pontuação = linhaScanner.nextInt();
    			int level = linhaScanner.nextInt();
    
    			temp.add(new HighScores(nome, pontuação, level));
    			linhaScanner.close();
    		}
    		scanner.close();
    
    		return temp;
    	}
    But I'am getting

    Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Update .txt File with Highscore

    NoSuchElement means you are calling a "next" method when there is nothing to read. Try printing out the contents of linha to see what value it actually holds. Does each line have a word and two ints?

  10. #10
    tcms is offline Member
    Join Date
    May 2013
    Posts
    43
    Rep Power
    0

    Default Re: Update .txt File with Highscore

    Quote Originally Posted by Junky View Post
    NoSuchElement means you are calling a "next" method when there is nothing to read. Try printing out the contents of linha to see what value it actually holds. Does each line have a word and two ints?
    I fixed, you were right. The problem was that the txt file didn't match with the scanner "search".

    Now I have to sort the Scores, but i have one question. The scores have: Score, Max level achieved and Money used. But I have to do it with priority, sort first by score, then by level and then by money. It's possible with the comparable and compareTo?

    Cheers

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default Re: Update .txt File with Highscore

    Quote Originally Posted by tcms View Post
    Now I have to sort the Scores, but i have one question. The scores have: Score, Max level achieved and Money used. But I have to do it with priority, sort first by score, then by level and then by money. It's possible with the comparable and compareTo?
    Sure it is; in the compareTo( ... ) method first compares the score fields; if they differ, return 1 or -1; if they are equal, compare the level fields; if they differ, return 1 or -1; if they are equal, compare the money fields.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    tcms is offline Member
    Join Date
    May 2013
    Posts
    43
    Rep Power
    0

    Default Re: Update .txt File with Highscore

    Quote Originally Posted by JosAH View Post
    Sure it is; in the compareTo( ... ) method first compares the score fields; if they differ, return 1 or -1; if they are equal, compare the level fields; if they differ, return 1 or -1; if they are equal, compare the money fields.

    kind regards,

    Jos
    Thanks for the help Jos, y're being very helpful.

    I made the code like this to compare first the score, then the level

    Java Code:
    	public int compareTo(HighScores o1) {
    		if (this.score== ((HighScores) o1).score) {
    			if (this.Level == ((HighScores) o1).Level) {
    				return 0;
    			} else if ((this.Level) > ((HighScores) o1).Level)
    				return 1;
    			else
    				return -1;
    
    		} else if ((this.score) > ((HighScores) o1).score)
    			return 1;
    		else
    			return -1;
    	}
    But is wrong.
    Last edited by tcms; 05-25-2013 at 08:37 PM.

  13. #13
    tcms is offline Member
    Join Date
    May 2013
    Posts
    43
    Rep Power
    0

    Default Re: Update .txt File with Highscore

    I updated to this:

    Java Code:
    	public int compareTo(HighScores o) {
    
    		HighScores other = (HighScores) o;
    
    		if (this.Level > other.Level) {
    			return 1;
    		} else if (this.Level < other.Level) {
    			return -1;
    		}
    
    		
    		if (this.Pontuação < other.Pontuação) {
    			return 1;
    		} else if (this.Pontuação > other.Pontuação) {
    			return -1;
    		}
    		
    		return 0;
    	}
    But now how can I use/test this? I don't understand How comparable works

Similar Threads

  1. Make a highscore. (HELP!)
    By Todai in forum New To Java
    Replies: 0
    Last Post: 09-20-2012, 05:19 PM
  2. Implementing Highscore for an App
    By Johnny22 in forum Advanced Java
    Replies: 6
    Last Post: 06-14-2011, 10:35 PM
  3. Highscore table for my game
    By richman92 in forum Java Applets
    Replies: 1
    Last Post: 02-26-2011, 05:21 AM
  4. update binary file from text file
    By billdef in forum New To Java
    Replies: 8
    Last Post: 09-02-2010, 09:24 AM
  5. Replies: 2
    Last Post: 05-11-2009, 10:07 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •