Need help with Java Program ASAP


#1

I have to read a file, write to the file, and be able to change it in a GUI, I can read just fine, but I can’t write to it PLEASE HELP
Code Below:
List newPlayer = new ArrayList<>();
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,%s%n",newPlayer.get(i).getName(), 
                    newPlayer.get(i).getPosition(),
                    newPlayer.get(i).getGames(),
                    newPlayer.get(i).getAtBats(),
                    newPlayer.get(i).getRuns(),
                    newPlayer.get(i).getHits(),
                    newPlayer.get(i).getWalks(),
                    newPlayer.get(i).getStrikeOuts(),
                    newPlayer.get(i).getBattingAvg());
            
        }
    } catch (FileNotFoundException ex) {
        JOptionPane.showMessageDialog(this, ex.getMessage());
        System.exit(3);
    }
    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));
}

/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    playerCombo = new javax.swing.JComboBox<>();
    jLabel1 = new javax.swing.JLabel();
    jLabel2 = new javax.swing.JLabel();
    jLabel3 = new javax.swing.JLabel();
    jLabel4 = new javax.swing.JLabel();
    jLabel5 = new javax.swing.JLabel();
    jLabel6 = new javax.swing.JLabel();
    jLabel7 = new javax.swing.JLabel();
    avgLabel = new javax.swing.JLabel();
    postionLabel = new javax.swing.JTextField();
    gamesLabel = new javax.swing.JTextField();
    atBatsLabel = new javax.swing.JTextField();
    runsLabel = new javax.swing.JTextField();
    hitsLabel = new javax.swing.JTextField();
    walksLabel = new javax.swing.JTextField();
    strikeOutsLabel = new javax.swing.JTextField();
    updateButton = new javax.swing.JButton();
    jLabel8 = new javax.swing.JLabel();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    addWindowListener(new java.awt.event.WindowAdapter() {
        public void windowClosing(java.awt.event.WindowEvent evt) {
            formWindowClosing(evt);
        }
    });

    playerCombo.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
    playerCombo.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            playerComboActionPerformed(evt);
        }
    });

    jLabel1.setText("Postion");

    jLabel2.setText("Games");

    jLabel3.setText("AtBats");

    jLabel4.setText("Runs");

    jLabel5.setText("Hits");

    jLabel6.setText("Walks");

    jLabel7.setText("Strike Outs");

    avgLabel.setText("0");

    gamesLabel.setText("0");

    atBatsLabel.setText("0");

    runsLabel.setText("0");

    hitsLabel.setText("0");

    walksLabel.setText("0");

    strikeOutsLabel.setText("0");

    updateButton.setText("Update");
    updateButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            updateButtonActionPerformed(evt);
        }
    });

    jLabel8.setText("Batting Average");

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addComponent(jLabel8)
                    .addGap(18, 18, 18)
                    .addComponent(avgLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel1)
                            .addGap(18, 18, 18)
                            .addComponent(postionLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(playerCombo, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 278, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGroup(layout.createSequentialGroup()
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                            .addComponent(jLabel7)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                            .addComponent(strikeOutsLabel))
                                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                            .addComponent(jLabel2)
                                            .addGap(18, 18, 18)
                                            .addComponent(gamesLabel))
                                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(jLabel4)
                                                .addComponent(jLabel5))
                                            .addGap(27, 27, 27)
                                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(runsLabel)
                                                .addComponent(hitsLabel)))
                                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                            .addComponent(jLabel6)
                                            .addGap(18, 18, 18)
                                            .addComponent(walksLabel)))
                                    .addGap(117, 117, 117)))
                            .addComponent(updateButton))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel3)
                            .addGap(18, 18, 18)
                            .addComponent(atBatsLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGap(0, 89, Short.MAX_VALUE)))
            .addContainerGap())
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(playerCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(updateButton))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 177, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel8)
                .addComponent(avgLabel))
            .addGap(18, 18, 18)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel1)
                .addComponent(postionLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(18, 18, 18)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel2)
                .addComponent(gamesLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                .addComponent(atBatsLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createSequentialGroup()
                    .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 23, Short.MAX_VALUE)
                    .addGap(7, 7, 7)))
            .addGap(18, 18, 18)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel4)
                .addComponent(runsLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(18, 18, 18)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel5)
                .addComponent(hitsLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(18, 18, 18)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel6)
                .addComponent(walksLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(18, 18, 18)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jLabel7)
                .addComponent(strikeOutsLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(18, 18, 18))
    );

    pack();
}// </editor-fold>                        

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

private void updateButtonActionPerformed(java.awt.event.ActionEvent 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;
    this.battingAVG = avgCalc(hits,atBats,walks);
    
}

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) {
    this.battingAVG = hits / (atBats - walks);
    return battingAVG;
}

}
Output:


#2

Have you tried a google search for some
== discussions / opinions ==
java how to write to a file site:stackoverflow.com
and read some articles…

or

== the Book ==
how to write to a file site:oracle.com

and if you are running this on a UNIX system
make sure you have the write-privlege for the file…!!