I need help with my java program, I have to read and write, but also update the
ID: 3867274 • Letter: I
Question
I need help with my java program, I have to read and write, but also update the data when I write too it, it reads, but when I try to write to it, the program resets, I need help
Code Below:
package baseball;
import java.awt.Font;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
/**
*
* @author Josh
*/
public class baseballForm extends javax.swing.JFrame {
List<player> 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));
}
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:
}
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new baseballForm().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JTextField atBatsLabel;
private javax.swing.JLabel avgLabel;
private javax.swing.JTextField gamesLabel;
private javax.swing.JTextField hitsLabel;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JLabel jLabel8;
private javax.swing.JComboBox<String> playerCombo;
private javax.swing.JTextField postionLabel;
private javax.swing.JTextField runsLabel;
private javax.swing.JTextField strikeOutsLabel;
private javax.swing.JButton updateButton;
private javax.swing.JTextField walksLabel;
// End of variables declaration
}
Class:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package baseball;
/**
*
* @author Josh
*/
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;
}
}
Explanation / Answer
Program resetting because you are calling write function on closing event, at that time form will close and data will be reset. To get rid of these, you need to keep separate close button and on click handle of this close button you need to write to file and then close form explicitly.
For update button, you can call write file method, then it updates data. Modified statement made as bold.
import java.awt.Font;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
/**
*
* @author Josh
*/
public class baseballForm extends javax.swing.JFrame {
List<player> 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));
}
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) {
writeFile();
}
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new baseballForm().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JTextField atBatsLabel;
private javax.swing.JLabel avgLabel;
private javax.swing.JTextField gamesLabel;
private javax.swing.JTextField hitsLabel;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JLabel jLabel8;
private javax.swing.JComboBox<String> playerCombo;
private javax.swing.JTextField postionLabel;
private javax.swing.JTextField runsLabel;
private javax.swing.JTextField strikeOutsLabel;
private javax.swing.JButton updateButton;
private javax.swing.JTextField walksLabel;
// End of variables declaration
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.