Ok I'm a VERY (stress the VERY) beginner programmer and I made a yahtzee game. Now, I'm trying to make a high score list so as to keep track of the high scores in my 1-player yahtzee game even after the applet is closed. I have one class called Yahtzee that runs my game of Yahtzee, and I would like to be able to access another class that would take care of keeping the high scores (possibly through writing to a text file or something). I have looked all over the internet, and every way I find to write to a text file only works in a stand-alone program, and fails as soon as I try to access it through my yahtzee program. I know my coding sucks, but here is my main Yahtzee class (I won't show my dieRoll, scoreCard, and ruleTest classes because I don't think there necessary for what I'm trying to do)

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Yahtzee extends JApplet implements ActionListener
	Paint p;
	dieRoll die1, die2, die3, die4, die5;
	JCheckBox check1,check2, check3, check4, check5;
	JLabel s1, s2, s3, s4, newline, newline2, newline3;
	scoreCard scorer;
	testRules tester;
	JButton roll, submit;
	public void init ()
		p = new Paint ();
		die1 = new dieRoll (20);
		die2 = new dieRoll (95);
		die3 = new dieRoll (170);
		die4 = new dieRoll (245);
		die5 = new dieRoll (320);
		scorer = new scoreCard();

		scorer.setVisible (true);

		roll = new JButton ("Roll");
		check1 = new JCheckBox ("Save");
		check2 = new JCheckBox ("Save");
		check3 = new JCheckBox ("Save");
		check4 = new JCheckBox ("Save");
		check5 = new JCheckBox ("Save");
		submit = new JButton ("Submit");
		s1 = new JLabel ("     ");
		s2 = new JLabel ("     ");
		s3 = new JLabel ("     ");
		s4 = new JLabel ("     ");
		newline = new JLabel ("                                                                                                                                                                                                                                                                                                                                                                                                              ");
		newline2 = new JLabel ("                                                                                                                                                                                                                                                                                                                                                                                                    ");
		newline3 = new JLabel ("                                                                                                                                                                                                                                                                                                                                                                                                              ");

		roll.addActionListener (this);
		check1.addActionListener (this);
		check2.addActionListener (this);
		check3.addActionListener (this);
		check4.addActionListener (this);
		check5.addActionListener (this);
		submit.addActionListener (this);

		check1.setEnabled (false);
		check2.setEnabled (false);
		check3.setEnabled (false);
		check4.setEnabled (false);
		check5.setEnabled (false);
		submit.setEnabled (false);

		setContentPane (p);
		p.add (check1);
		p.add (s1);
		p.add (check2);
		p.add (s2);
		p.add (check3);
		p.add (s3);
		p.add (check4);
		p.add (s4);
		p.add (check5);
		p.add (newline);
		p.add (newline2);
		p.add (newline3);
		p.add (roll);
		p.add (submit);
	public void actionPerformed (ActionEvent e)
		Object obj = e.getSource ();
		if (obj == roll)
			if (check1.isSelected())die1.save();
			if (check2.isSelected())die2.save();
			if (check3.isSelected())die3.save();
			if (check4.isSelected())die4.save();
			if (check5.isSelected())die5.save();
			check1.setEnabled (true);
			check2.setEnabled (true);
			check3.setEnabled (true);
			check4.setEnabled (true);
			check5.setEnabled (true);
			repaint ();
		if (die1.getRolesLeft() == 0)
			check1.setSelected (false);
			check2.setSelected (false);
			check3.setSelected (false);
			check4.setSelected (false);
			check5.setSelected (false);
			submit.setEnabled (true);
			check1.setEnabled (false);
			check2.setEnabled (false);
			check3.setEnabled (false);
			check4.setEnabled (false);
			check5.setEnabled (false);
			roll.setEnabled (false);
		if (obj == submit && scorer.testSelected() == true)
			tester = new testRules(die1.getRoll(), die2.getRoll(), die3.getRoll(), die4.getRoll(), die5.getRoll());
			submit.setEnabled (false);
			roll.setEnabled (true);
			if (scorer.getSelected() == 1)scorer.setScore(tester.Ace(), 1);
			if (scorer.getSelected() == 2)scorer.setScore(tester.Two(), 2);
			if (scorer.getSelected() == 3)scorer.setScore(tester.Three(), 3);
			if (scorer.getSelected() == 4)scorer.setScore(tester.Four(), 4);
			if (scorer.getSelected() == 5)scorer.setScore(tester.Five(), 5);
			if (scorer.getSelected() == 6)scorer.setScore(tester.Six(), 6);
			if (scorer.getSelected() == 7)scorer.setScore(tester.ThreeKind(), 7);
			if (scorer.getSelected() == 8)scorer.setScore(tester.FourKind(), 8);
			if (scorer.getSelected() == 9)scorer.setScore(tester.FullHouse(), 9);
			if (scorer.getSelected() == 10)scorer.setScore(tester.SmallStraight(), 10);
			if (scorer.getSelected() == 11)scorer.setScore(tester.LargeStraight(), 11);
			if (scorer.getSelected() == 12)scorer.setScore(tester.Yahtzee(), 12);
			if (scorer.getSelected() == 13)scorer.setScore(tester.Chance(), 13);
			if (die1.getTurns() == 1)
				Writer hscore = new Writer ();
				String[] arguments = new String[] {"123"};
	public class Paint extends JPanel
		public void paintComponent (Graphics page)
			super.paintComponents (page);
			die1.paint (page);
			die2.paint (page);
			die3.paint (page);
			die4.paint (page);
			die5.paint (page);
			setBackground (Color.yellow);

And yes I know its not great but, oh well, if theres any way for me to do it let me know.

If you make a new class that holds your high scores and it implements java.io.Serializable, you can easily write it to a file and recover it intact later on; this takes out the mess of reading and parsing a file.

This article has been dead for over six months. Start a new discussion instead.