-1

Hi I am currently working on a game in Java, that tests the users ability to work better with our without music for analyzing. I am using write file and created a .txt file to save the results. I was unsure how do i create headings for the saved results such as First name: Second Name. I am also a little lost as to calculate a percentage from the results. The results are stored in variables, Score and Score 2 i simply would like to display the percentage of the results. Thank you.

if(Globals.score2 > Globals.score) {
    //Better with music
    MessageBox.infoBox("You scored " + Globals.score + " without music & " + Globals.score2 + " with music!\n" + "You seemed to concentrate better while music was playing!" , "Score");
} else if (Globals.score2 < Globals.score) {
    //Better Without Music
    MessageBox.infoBox("You scored " + Globals.score + " without music & " + Globals.score2 + " with music!\n" + "You seemed to concentrate better without any music playing!" , "Score");
} else {
    //Equal Reults
    MessageBox.infoBox("You scored " + Globals.score +  " without music & " + Globals.score2 + " with music!\n" + "Your concentration stayed the same through each play through!" , "Score");
}  
// JOptionPane.showMessageDialog(frame, "Your score for game one was" + "" + Globals.score2 );
new FinalScore().setVisible(true);
this.dispose();
AudioPlayer.player.stop(as);

toolkit.beep();
timer.cancel(); //Not necessary because we call System.exit
//System.exit(0); //Stops the AWT thread (and everything else)

//CODE FOR WRITING TO FILE

public class WriteFile {
    public static void SaveGame() throws IOException {
        String content = (Globals.IDUser + ", " + Globals.firstName + ", " + Globals.lastName + ", " + Globals.userAge + ", " + Globals.eThnic + ", " + Globals.maleOrFem  + ", " + " In the first game you scored, " + Globals.score + ", " + " In the second game you scored." + Globals.score2);
        File file = new File("ResultsSaves.txt");

        // if file exists, then overwrite current saved file with new
        if (!file.exists()) {
            file.createNewFile();
        }

        FileWriter fw = new FileWriter(file.getAbsoluteFile(), true);
        BufferedWriter bw = new BufferedWriter(fw);
        bw.write(content);
        bw.newLine();
        bw.close(); 

        JOptionPane.showMessageDialog(null, "Your details have been saved!");   

        }
    }
}
Kulu Limpa
  • 3,501
  • 1
  • 21
  • 31
  • So where is the code that writes to the file? Or is your question how you can write to a file in Java, in which case there are probably already a lot of answers, e.g., http://stackoverflow.com/questions/2885173/java-how-to-create-a-file-and-write-to-a-file – Kulu Limpa May 04 '15 at 18:57
  • @KuluLimpa Hi, i have added the code that I used to extract to a text file above. – Whitena Cording May 04 '15 at 19:42

1 Answers1

0

So basically you ask two things:

How to calculate the percentage of the score?

You don't really say a percentage of what, but I assume you want to calculate the fraction of score from score + score2 (and analogous for score2).

I'm going to assume that Globals.score and Globals.score2 are both of type double. Therefore, the percentage would be computed as

double scorePercentage = 100 * Globals.score / (Globals.score + Globals.score2)

How to print a heading 'row' before saving the game results?

Well... really just the same as you save your game results. It's even easyier as the heading is statically known.

However, if I am not mistaken, creating a separate BufferedWriter for printing the heading and the results would not append to the file but overwrite the previous output. My suggestion is to instantiate the BufferedWriter as part of WriteFile's initialization. The WriteFile class could look something like this:

public final class WriteFile {

    private WriteFile(){/* This is a class with only static methods - make the constructor private to prohibit instantiation */}

    public static final String heading = "UserID, First Name, Last Name, Age, Ethnic, Gender, Score Game 1, Score Game 2";

    private static BufferedWriter bufferedFileWriter = null;

    static {
        File file = new File("ResultsSaves.txt");

        // if file exists, then overwrite current saved file with new
        if (!file.exists()) {
            file.createNewFile();
        }

        FileWriter fw = new FileWriter(file.getAbsoluteFile(), true);
        bufferedFileWriter = new BufferedWriter(fw);
    }

    private static String getGameResult() {
        return Globals.IDUser + ", " + Globals.firstName + ", " + Globals.lastName + ", " + Globals.userAge + ", " + Globals.eThnic + ", " + Globals.maleOrFem  + ", " + " In the first game you scored, " + Globals.score + ", " + " In the second game you scored." + Globals.score2; 
    }

    /** Not really needed, but there to stay true to the original design */
    public static void printHeading() throws IOException {
        save(heading); 
    }

    /** Not really needed, but there to stay true to the original design */
    public static void saveGame() throws IOException {
        save(getGameResult()); 
    }

    public static void save(String content) throws IOException {

        bufferedFileWriter.write(content);
        bufferedFileWriter.newLine();
        // flush to get the contents stored to file
        bufferedFileWriter.flush();  

        JOptionPane.showMessageDialog(null, "Your details have been saved!");   

        }
    }

    /** Don't close the writer before all desired content is written. */
    public static void close() throws IOException {
        bufferedFileWriter.close(); 
    }
}

Friendly advice: Be careful when using global variables. They can become a hell of a debugging nightmare. You should prefer passing them as parameters to your methods.

Kulu Limpa
  • 3,501
  • 1
  • 21
  • 31