File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ArrayList help needed

 
megan fosikski
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Having a problem with my arrayList. I get data from a jtext box that the player adds in. than i save the data to the correct players list. It all works, but overwrites the users score added, i want a player to hit save button to add number to the list so i can average the out at a later time.
heres some snipets of player1, i have 4 players in all.

//heres player 1 save

private void saveActionPerformed(java.awt.event.ActionEvent evt) {

// gets number from text box
String scores = diffText.getText();
diffText.setText("");

// create ArrayList, adds number into an it
ArrayList<String> list = new ArrayList<String>();
list.add(0,scores);

// print lines to see whats going on
System.out.println(" SaveButon score is " + scores);
System.out.println(" SaveButton list is " + list);

// Get player selected
String player = (String) golferSaved.getSelectedItem();
golfer.setText(" " + player);
System.out.println("this is: " + player);

// Save selected player and add score, than save
if (player.equals("Player 1")) {
try {
System.out.println("Inside save player1"+ list.toString());
FileOutputStream fos = new FileOutputStream("p1.dat");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(list);
oos.close();
} catch (Exception e) {
e.printStackTrace();
}


// heres player 1 load

private void loadActionPerformed(java.awt.event.ActionEvent evt) {

String name = (String) golferSaved.getSelectedItem();
golfer.setText(" " + name);
String player = (String) golferSaved.getSelectedItem();
golfer.setText(" " + player);
System.out.println("this is: " + player);

if (player.equals("Player 1")) {
try {
FileInputStream fis = new FileInputStream("p1.dat");
ObjectInputStream ois = new ObjectInputStream(fis);
List<String> list = (ArrayList) ois.readObject();
for (String s : list) {
System.out.println(s);
}
ois.close();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Player is: " + player);


ps, im using netbeans gui builder, user adds a few numbers into 3 textboxes, i do a math equation, save than add new number to arraylist for it to be saved each time with correct player selected.
 
W. Joe Smith
Ranch Hand
Posts: 710
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A few things:

1. Please place code inside code tags. It makes it much easier to read. You can do this when writing your post using the "Code" button above the main entry box.

2. We have a policy here to use a real sounding name. If your name really is Megan Fox that is fine, but odds are a sheriff or bartender will be sending you a PM about it soon.

3. I think your problem is with this line:



You are adding the score to the 0 index each time, which is overwritting the current one at the 0 index. If you just use list.add(scores); it will add the new item to the end of the ArrayList by default.
 
Rob Spoor
Sheriff
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
W. Joe Smith wrote:2. We have a policy here to use a real sounding name. If your name really is Megan Fox that is fine, but odds are a sheriff or bartender will be sending you a PM about it soon.

"megan", Joe was referring to our naming policy. Joe, no PM this time

3. I think your problem is with this line:



You are adding the score to the 0 index each time, which is overwritting the current one at the 0 index. If you just use list.add(scores); it will add the new item to the end of the ArrayList by default.

The indexed add method does not overwrite anything. This call always adds an element at the start of the list.
 
W. Joe Smith
Ranch Hand
Posts: 710
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed it does Rob....indeed it does.

OK, so then I don't know what is happening.....
 
megan fosikski
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
changed name..most cant pronounce it, so they call it fox..its a nickname

but yes, ive tried without out 0, with 0, with i and for loop making i++ still overwrites
 
Rob Spoor
Sheriff
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Every time you create a new ArrayList, add one String, then overwrite the entire file with only the serialized ArrayList.

Instead of serializing and deserializing an ArrayList, why not simply use a plain text file with each score on one line? You then create a FileWriter with the append flag set to true, write the command with a trailing line break to it, then close the FileWriter. For reading you wrap a FileReader in a BufferedReader and use its readLine() method until that returns null.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic