File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes ArrayList help needed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "ArrayList help needed" Watch "ArrayList help needed" New topic
Author

ArrayList help needed

megan fosikski
Greenhorn

Joined: Apr 19, 2010
Posts: 2
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

Joined: Feb 10, 2009
Posts: 710
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.


SCJA
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19538
    
  16

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
W. Joe Smith
Ranch Hand

Joined: Feb 10, 2009
Posts: 710
Indeed it does Rob....indeed it does.

OK, so then I don't know what is happening.....
megan fosikski
Greenhorn

Joined: Apr 19, 2010
Posts: 2
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

Joined: Oct 27, 2005
Posts: 19538
    
  16

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ArrayList help needed
 
Similar Threads
calling servlet from java program
Could somebody explain this to me?
how FileOutputStream and ObjectOutputStream working here
Deserializing
java.io.EOFException using readObject