1 Replies - 231 Views - Last Post: 21 January 2013 - 12:07 PM Rate Topic: -----

#1 Buttertoast  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 60
  • Joined: 10-January 13

Highscore Top 10 FileWriter/Reader

Posted 21 January 2013 - 11:57 AM

Hello,
i would like to add a highscore list to my sudoku game,
to save all scores i created a filewriter and wrote the scores into the .txt
but how can i manage to get the best times on the top? i thought of bubble sort, but i dont know how to only get the times back from the .txt

bubble sort
 void bubbleSort ()
{
for (int end = 10; end > 0;ende--)
{
  for (int i = 0; i < end; i++)
 {
    if (scores[i]>scores[i+1])
    {
          int ablage = scores[i];
          scores[i] = scores[i+1];
          scores[i+1] = ablage;
     }
   }
 }


File Writer
try{
            // Create file 
            BufferedWriter out = new BufferedWriter(new FileWriter("out.txt"));
            out.write("\t\t\tBestzeiten Top 10");
            out.newLine();
            String platz1 = " 1." + uhr.getText();
            out.write(platz1);
            out.newLine();
            out.write(" 2.");
            out.newLine();
            out.write(" 3.");
            out.newLine();
            out.write(" 4.");
            out.newLine();
            out.write(" 5.");
            out.newLine();
            out.write(" 6.");
            out.newLine();
            out.write(" 7.");
            out.newLine();
            out.write(" 8.");
            out.newLine();
            out.write(" 9.");
            out.newLine();
            out.write("10.");
            //Close the output stream
            out.close();
        }catch (Exception e){//Catch exception if any
            System.err.println("Error: " + e.getMessage());
        }
}


FileReader
try{
            BufferedReader br = new BufferedReader(new FileReader("out.txt"));            
            for(int r=0; r<11;r++){
                
             String zeile = br.readLine();
             System.out.println(zeile);
             System.out.println("");
            }

            br.close();
            }catch (Exception e){//Catch exception if any
            System.err.println("Error: " + e.getMessage());
              }



Is This A Good Question/Topic? 0
  • +

Replies To: Highscore Top 10 FileWriter/Reader

#2 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,689
  • Joined: 13-March 10

Re: Highscore Top 10 FileWriter/Reader

Posted 21 January 2013 - 12:07 PM

What is the point in writing:
out.write("1");
out.write("2");
out.write("3");


If you had to keep going till 1 million then it would take you about 10 minutes.
Create a data structure of your choice that will hold the values and loop through it.
String[] scoresArray = new String[]{"1","2","3","4"};
for(String score: scoresArray){
  out.write(score)
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1