I created a game a random guessing game ( for school project ) . I just want to ask on how to put a hall of fame? Like when you played the game then its end it will be putted his/her name then it will be saved. Then it will be based on which is greater than. Just tell me what should i need to took. Then i will make an algorithm on it! But if your soo kind you can make it LOL :)) . THANKS ALOT.

Dani AI

Generated

A compact, practical approach that builds on ’s suggestion: store both name and score as a single line per entry in one file, append new results (don’t overwrite), and sort when the hall of fame is shown. That fixes the current pattern in ’s sample (two files being rewritten) and keeps reads/writes simple.

Example: append a sanitized, delimited line so each record is one line in the file.

try (BufferedWriter out = new BufferedWriter(new FileWriter("hall.txt", true))) {
    out.write(name.replace("|", " ") + "|" + score + System.lineSeparator());
}

When displaying the hall of fame, read every line, parse into small objects (name + int score), sort by score descending, then show the top N. A minimal read/sort flow (Entry class assumed) looks like:

List<Entry> list = new ArrayList<>();
try (BufferedReader in = new BufferedReader(new FileReader("hall.txt"))) {
    String line;
    while ((line = in.readLine()) != null) {
        String[] p = line.split("\\|");
        list.add(new Entry(p[0], Integer.parseInt(p[1])));
    }
}
Collections.sort(list, new Comparator<Entry>() {
    public int compare(Entry a, Entry b) {
        return Integer.compare(b.score, a.score);
    }
});
// display list.subList(0, Math.min(10, list.size()));

Practical notes: pick a delimiter that won’t appear in names (or escape it), validate/trim parsed fields and handle NumberFormatException, and use try-with-resources (shown) so streams close reliably. To prevent unbounded file growth, keep only the top N in memory and rewrite the file (open FileWriter without append) after sorting. For multi-threaded or networked scenarios consider synchronization or a small database; for a simple school project the single-file append + read-and-sort pattern is the quickest, clearest solution.

Recommended Answers

All 10 Replies

I created a game a random guessing game ( for school project ) . I just want to ask on how to put a hall of fame? Like when you played the game then its end it will be putted his/her name then it will be saved. Then it will be based on which is greater than. Just tell me what should i need to took. Then i will make an algorithm on it! But if your soo kind you can make it LOL :)) . THANKS ALOT.

Hmm why not save the score at the end of the game including the players name to a text file, then on start up or when the user presses to ask to see the hall of fame you can read in the entries from the text file into two arrays one for the name, another for the score, (or maybe a 2d array) and list them according to their high scores, that will be done by a simple if else statement checking the condition if the one is greater or less then the other and saving the value that is greater to a new array which will hold the sorted values of the scores-highest to lowest. That's how i would do it, though there are many alternatives, but one thing will remain constant, the storing of the scores in a file and reading from the file etc

Hmm why not save the score at the end of the game including the players name to a text file, then on start up or when the user presses to ask to see the hall of fame you can read in the entries from the text file into two arrays one for the name, another for the score, (or maybe a 2d array) and list them according to their high scores, that will be done by a simple if else statement checking the condition if the one is greater or less then the other and so on

How ?? I'm still new on java language :((((((((((

How ?? I'm still new on java language :((((((((((

well Google will always help hey but to start you off: heres a link to read text files:http://www.roseindia.net/java/beginners/java-read-file-line-by-line.shtml then here is another link that will take the reading of the file one step further(putting it into an array):http://www.java-forums.org/new-java/7001-reading-text-file-into-array.html, and finally here is a link on writing to text files:http://www.abbeyworkshop.com/howto/java/writeText/index.html and then here just for the cherry on top is a small example of comparing variables for Highest and lowest values:http://www.roseindia.net/java/beginners/largernumber.shtml
Hope its enough to get you started...

well google will always help hey but to start you off: Heres a link to read text files:http://www.roseindia.net/java/beginners/java-read-file-line-by-line.shtml then here is another link that will take the reading of the file one step further(putting it into an array):http://www.java-forums.org/new-java/7001-reading-text-file-into-array.html, and finally here is a link on writing to text files:http://www.abbeyworkshop.com/howto/java/writetext/index.html and then here just for the cherry on top is a small example of comparing variables for highest and lowest values:http://www.roseindia.net/java/beginners/largernumber.shtml
hope its enough to get you started...

thanks alot dude!!

How do i add a new line at this code? Like i want to put a new string/variable in the same text file?
Here's my code. I still don't have an idea how would i put it.

import java.io.*;

public class WriteTextFileExample{
  public static void main(String[] args)throws IOException{
      int anArray[]  = new int[100];
  BufferedReader guess= new BufferedReader(new InputStreamReader(System.in));
  Writer output = null;
  System.out.print("Write your name: ");
  String a= guess.readLine();
  String text = a;
  File file = new File("Name.txt");
  output = new BufferedWriter(new FileWriter(file));
  output.write(text);
  output.close();
  System.out.println("Your file has been written");  
 
  System.out.print("Write your name: ");
  String number = guess.readLine();
  String numberdefine = number;
  File scores = new File("Scores.txt");
  output = new BufferedWriter(new FileWriter(scores));
  output.write(numberdefine);
  output.close();
  System.out.println("Your file has been written");  
         
  
  }
}

How do i add a new line at this code? Like i want to put a new string/variable in the same text file?
Here's my code. I still don't have an idea how would i put it.

import java.io.*;

public class WriteTextFileExample{
  public static void main(String[] args)throws IOException{
      int anArray[]  = new int[100];
  BufferedReader guess= new BufferedReader(new InputStreamReader(System.in));
  Writer output = null;
  System.out.print("Write your name: ");
  String a= guess.readLine();
  String text = a;
  File file = new File("Name.txt");
  output = new BufferedWriter(new FileWriter(file));
  output.write(text);
  output.close();
  System.out.println("Your file has been written");  
 
  System.out.print("Write your name: ");
  String number = guess.readLine();
  String numberdefine = number;
  File scores = new File("Scores.txt");
  output = new BufferedWriter(new FileWriter(scores));
  output.write(numberdefine);
  output.close();
  System.out.println("Your file has been written");  
         
  
  }
}

to add new line:

output.write("\r\n\r\n");

Which part?

Which part?

the above code is to add a new line

String variable="yup im it :)";
...
 output = new BufferedWriter(new FileWriter(scores));
  output.write(numberdefine);
output.write("\r\n\r\n");//new line
output.write(variable);//here is the new string you want to write
  output.close();
...

How do i make that everytime someone wins they will save the name and the score without using another Write Text File? HEELP.

How do i make that everytime someone wins they will save the name and the score without using another Write Text File? HEELP.

Can you elaborate further?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.