Help in writing an array of Object Player to File

J

justin

I am writing a Hi-Score GUI whereby it is suppose to store an array of
10 players when the input name comes up. But I am having problems
doing it was wondering if anyone can help thanks.Below is a snippet of
my codes when doign ObjectStreams. BTW when trying out in terms
writing not as an Object but as a textFile this works so yeah the only
problem was it wouldn't append so if anyone could show me how as well
it would be much appreciated. Thanks

void okButton_actionPerformed(ActionEvent e) throws (<-------- this
is in my PlayerInputGUI which is the GUI whereby players enter their
names onto a textField)

FileNotFoundException,IOException {
File scoreFile = new File("Scores.dat");
FileOutputStream outFileStream = new FileOutputStream(scoreFile);
ObjectOutputStream outObjectStream = new
ObjectOutputStream(outFileStream);
PlayerDetails players[]= null;
for(int i=0;i<10;i++){
players = new PlayerDetails(inputField.getText());
outObjectStream.writeObject(players);
}
outObjectStream.close();
this.hide();
}

void highScore_actionPerformed(ActionEvent e) throws
FileNotFoundException, IOException,
ClassNotFoundException < ---- (THIS IS IN THE OTHER GUI THE MAIN
GAME GUI, this basically functions to display the scores on the
JOptionPane){
File inFile = new File("Scores.dat");
FileInputStream inFileStream = new FileInputStream(inFile);
ObjectInputStream inObjectStream = new
ObjectInputStream(inFileStream);

PlayerDetails players;
for (int i=0;i<10;i++){
players=(PlayerDetails)inObjectStream.readObject();
JOptionPane.showMessageDialog(null,"1 "+players.getName());
(WHILST MEDDLING AROUND I GOT AN ERROR stating that
"MovingTilesApp.java": getName(java.lang.String) in
sg_board.PlayerDetails cannot be applied to () at line 261, column 59
& I DON'T KNOW WHY)
inObjectStream.close();
}
}

AND LASTLY MY PlayerDetails Class

import java.io.*;

public class PlayerDetails implements Serializable{
private String name;
//public static void main(String[] args) {

public PlayerDetails( String n){
name = n;
}
public void setName(String n) {
name = n;
}
public void getName (String n){
name = n;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top