Results 1 to 2 of 2

Thread: Need help with java program

  1. #1
    Join Date
    Nov 2008
    Location
    JAVA UNIVERSITY
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Need help with java program

    I have a program to make that involves me creating a text file that holds the top five scores from a guessing number game. i have to display the top five scores in order from greatest to least before the game starts. then the game starts and after the game ends the score from the user will be saved in the file and will go through the process of where it stands in the order of the scores.

    when reading the input file in i have to display it through an array code also.

    && the output file for the top five scores has to write over the old score file instead of creating a new file.

    omg i need help badly...please anybody

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    import java.io.*;
    import java.util.ArrayList;
    
    public class ScoreFile extends File {
      public ScoreFile(String path) {
        super(path);
      }
    
      public Score[] readScores() {
        BufferedInputReader in = new BufferedReader(new FileReader(this));
        String ln;
        ArrayList<Score> r;
    
        while ((ln = in.readLine()) != null)
          r.add(Score.fromLine(ln));
    
        in.close();
    
        return r.toArray();
      }
    
      public void writeScores(Score[] scores) {
        BufferedWriter out = new BufferedWriter(new FileWriter(this));
    
        for (Score score : scores) {
          out.write(score.toLine());
          out.newLine();
        }
    
        out.close();
      }
    
      public void printScores() {
        Score[] scores = readScores();
    
        for (Score score : scores)
          System.out.writeln(score.prettyPrint());
      }
    }
    Code:
    public class Score {
      public int score;
      public Player player;
    
      public Score(Player player, int score) {
        this.player = player;
        this.score = score;
      }
    
      public static Score fromLine(String line) {
        String[] words;
        StringBuilder name = new StringBuilder();
        int i;
    
        try {
          words = line.split(" ");
        } catch (PatternSyntaxException e) {}
    
        for (i = 0; i < words.length - 1; ++i)
          name.append(words[i] + (i == words.length - 2 ? "" : " "));
    
        return new Score(name, Integer.parseInt(words[i]));
      }
    
      public String toLine() {
        return this.name + " " + this.score;
      }
    
      public String prettyPrint() {
        return this.name + ":\t" + this.score;
      }
    }
    Haskell equivalent:
    Code:
    type Player = String
    data Score = Score Player Integer
      deriving (Show, Read)
    
    readScores :: FilePath -> IO [Score]
    readScores p = readFile p >>= return . map read . lines
    
    writeScores p = writeFile p . map show
    
    printScores p = readFile p >>= mapM_ (putStrLn . formatScore) lines
      where formatScore (Score p s) = p ++ ":\t" ++ show s
    Both untested.
    Last edited by Twey; 11-20-2008 at 04:20 AM. Reason: Add warning.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

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