Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help with my Java Program, I have it mostly working, I\'m trying to read

ID: 3867183 • Letter: I

Question

I need help with my Java Program, I have it mostly working, I'm trying to read and write to a file, it sort of works, when ever I use my program to write to the file it overwrites everything, I want to t stop it Code Below:

List<player> newPlayer = new ArrayList<player>();
DefaultComboBoxModel playerDrop = new DefaultComboBoxModel();

public void fillBox() {
playerDrop.removeAllElements();
playerDrop.addElement("Choose Player");
for (int i = 0; i < newPlayer.size(); i++) {
playerDrop.addElement(newPlayer.get(i).getName());
}
}

public void readFile() {
Scanner readFile = null;
File baseballFile = new File("c:\Data\pirates_stats.csv");
String aLine = "";
String[] fields;
player aPlayer = new player();

try {
readFile = new Scanner(baseballFile);
while (readFile.hasNext()) {
aLine = readFile.nextLine();
fields = aLine.split(",");

aPlayer = new player(fields[0], fields[1],
Integer.parseInt(fields[2]),
Integer.parseInt(fields[3]),
Integer.parseInt(fields[4]),
Integer.parseInt(fields[5]),
Integer.parseInt(fields[6]),
Integer.parseInt(fields[7]));
newPlayer.add(aPlayer);
}
} catch (FileNotFoundException ex) {
JOptionPane.showMessageDialog(this, ex.getMessage(), "File Error", JOptionPane.ERROR_MESSAGE);
System.exit(2);
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(this, ex.getMessage(), "File Error", JOptionPane.ERROR_MESSAGE);
System.exit(3);
}
readFile.close();
}

public void writeFile() {
PrintWriter writePlayer = null;
try {
writePlayer = new PrintWriter("c:\Data\pirates_stats.csv");
for (int i = 0; i < newPlayer.size(); i++) {
writePlayer.printf("%s,%s,%s,%s,%s,%s,%s,%s%n",newPlayer.get(i).getName(),
postionLabel.getText(),
gamesLabel.getText(),
atBatsLabel.getText(),
runsLabel.getText(),
hitsLabel.getText(),
walksLabel.getText(),
strikeOutsLabel.getText());
  
}
} catch (FileNotFoundException ex) {
JOptionPane.showMessageDialog(this, ex.getMessage());
System.exit(1);
}
writePlayer.close();
}

private void playerComboActionPerformed(java.awt.event.ActionEvent evt) {
double atBats;
double walks;
double hits;
double average;
  
// TODO add your handling code here:
String choice = (String) this.playerCombo.getSelectedItem();
player newPlay = new player();
for (int x = 0; x < newPlayer.size(); x++) {
if (choice.equalsIgnoreCase(newPlayer.get(x).getName())) {
this.postionLabel.setText(newPlayer.get(x).getPosition());
this.atBatsLabel.setText("" + newPlayer.get(x).getAtBats());
this.gamesLabel.setText("" + newPlayer.get(x).getGames());
this.hitsLabel.setText("" + newPlayer.get(x).getHits());
this.runsLabel.setText("" + newPlayer.get(x).getRuns());
this.walksLabel.setText("" + newPlayer.get(x).getWalks());
this.strikeOutsLabel.setText("" + newPlayer.get(x).getStrikeOuts());
atBats = (newPlayer.get(x).getAtBats());
walks = (newPlayer.get(x).getWalks());
hits = (newPlayer.get(x).getHits());
average = newPlay.avgCalc(hits, atBats, walks);
this.avgLabel.setText(String.format("%.2f",average));
return;
  
  
}
  
}

}   

private void formWindowClosing(java.awt.event.WindowEvent evt) {   
// TODO add your handling code here:
writeFile();
}

public class player {

private String name;
private String position;
private int games;
private int atBats;
private int runs;
private int hits;
private int walks;
private int strikeOuts;
private double battingAVG;

public String getName() {
return name;
}

public String getPosition() {
return position;
}

public int getGames() {
return games;
}

public int getAtBats() {
return atBats;
}

public int getRuns() {
return runs;
}

public int getHits() {
return hits;
}

public int getWalks() {
return walks;
}

public int getStrikeOuts() {
return strikeOuts;
}

public double getBattingAvg() {
return battingAVG;
}

public void setName(String name) {
this.name = name;
}

public void setPosition(String position) {
this.position = position;
}

public void setGames(int games) {
this.games = games;
}

public void setAtBats(int atBats) {
this.atBats = atBats;
}

public void setRuns(int runs) {
this.runs = runs;
}

public void setHits(int hits) {
this.hits = hits;
}

public void setWalks(int walks) {
this.walks = walks;
}

public void setStrikeOuts(int strikeOuts) {
this.strikeOuts = strikeOuts;
}

public player(String name, String position, int games, int atBats, int runs, int hits, int walks, int strikeOuts) {
this.name = name;
this.position = position;
this.games = games;
this.atBats = atBats;
this.runs = runs;
this.hits = hits;
this.walks = walks;
this.strikeOuts = strikeOuts;
  
}

public player() {
}

@Override
public String toString() {
return "player{" + "name=" + name + ", position=" + position + ", games=" + games + ", atBats=" + atBats + ", runs=" + runs + ", hits=" + hits + ", walks=" + walks + ", strikeOuts=" + strikeOuts + '}';
}

public double avgCalc(double hits, double atBats, double walks) {
return battingAVG = hits / (atBats - walks);
}
}

Explanation / Answer

If the problem is just to stop overwriting We can easily resolve it by change below: -

Change this line : -

writePlayer = new PrintWriter("c:\Data\pirates_stats.csv");

to this: -

writePlayer = new PrintWriter(new FileWriter("c:\Data\pirates_stats.csv", true));

FileWriter(File file, boolean append). The second param indicates whether the file is opened in append mode

Note that I cannot run your code and make changes in your code and paste running code screenshot, because you have not provided the full code. Let me know if you still face issues.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote