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: 3867200 • 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:

import java.awt.Font;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;

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();
}

public baseballForm() {
initComponents();
readFile();
fillBox();
this.playerCombo.setModel(playerDrop);
this.atBatsLabel.setFont(new Font("Times New Roman", 20,30));
this.avgLabel.setFont(new Font("Times New Roman", 20,30));
this.gamesLabel.setFont(new Font("Times New Roman", 20,30));
this.hitsLabel.setFont(new Font("Times New Roman", 20,30));
this.jLabel1.setFont(new Font("Times New Roman", 20,30));
this.jLabel2.setFont(new Font("Times New Roman", 20,30));
this.jLabel3.setFont(new Font("Times New Roman", 20,30));
this.jLabel4.setFont(new Font("Times New Roman", 20,30));
this.jLabel5.setFont(new Font("Times New Roman", 20,30));
this.jLabel6.setFont(new Font("Times New Roman", 20,30));
this.jLabel7.setFont(new Font("Times New Roman", 20,30));
this.playerCombo.setFont(new Font("Times New Roman", 20,30));
this.postionLabel.setFont(new Font("Times New Roman", 20,30));
this.runsLabel.setFont(new Font("Times New Roman", 20,30));
this.strikeOutsLabel.setFont(new Font("Times New Roman", 20,30));
this.updateButton.setFont(new Font("Times New Roman", 20,30));
this.walksLabel.setFont(new Font("Times New Roman", 20,30));
this.jLabel8.setFont(new Font("Times New Roman", 20,30));
}

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);
}

Output:

Update Josh Bell Batting Average 0.16 Postion 1B Games 11 AtBats 32 Runs 1 Hits 5 Walks1 Strike Outs3

Explanation / Answer

change yur writeFile function to below:

public void writeFile() {

BufferedWriter bfWriter = null;

PrintWriter writePlayer = null;

try {

// the second paramter here tells the writer to append to file and not to overwrite

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

writePlayer = new PrintWriter(bfWriter);

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);

}

if(writePlayer != null) {

writePlayer.close();

}

}

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