Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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,

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 newPlayer = new ArrayList(); DefaultComboBoxModel playerDrop = new DefaultComboBoxModel();

public void fillBox() { playerDrop.removeAllElements(); playerDrop.addElement("Choose Player"); for (int i = 0; i

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

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

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

image text in transcribed

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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle Database 11g SQL

Authors: Jason Price

1st Edition

0071498508, 978-0071498500

More Books

Students also viewed these Databases questions

Question

How do Data Types perform data validation?

Answered: 1 week ago

Question

How does Referential Integrity work?

Answered: 1 week ago