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

I need help with Java, I have to read and write to a file, that part works, but

ID: 3866970 • Letter: I

Question

I need help with Java, I have to read and write to a file, that part works, but how do I use the information to calculate the average using a class and GUI? My code is 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(",");
//handle the integer
aPlayer = new player(fields[0],fields[1],fields[2],fields[3],fields[4],fields[5],fields[6],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 ", 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());
//%n is the windows /n
}

} catch (FileNotFoundException ex) {
JOptionPane.showMessageDialog(this, ex.getMessage());
System.exit(1);
}

writePlayer.close();
}
public baseballForm() {
initComponents();
readFile();
fillBox();
this.playerCombo.setModel(playerDrop);
}

private void playerComboActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String choice = (String) this.playerCombo.getSelectedItem();
  
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());
break;
}
}
}   

public class player {
private String name;
private String position;
private String games;
private String atBats;
private String runs;
private String hits;
private String walks;
private String strikeOuts;
private double battingAVG;

public String getName() {
return name;
}

public String getPosition() {
return position;
}

public String getGames() {
return games;
}

public String getAtBats() {
return atBats;
}

public String getRuns() {
return runs;
}

public String getHits() {
return hits;
}

public String getWalks() {
return walks;
}

public String getStrikeOuts() {
return strikeOuts;
}

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

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

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

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

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

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

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

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

public player(String name, String position, String games, String atBats, String runs, String hits, String walks, String 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 = battingAVG;
}

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

Explanation / Answer

To solve the question, we must first change the data type of fields in player from String to int. This will help to calculate average. I have changed the player fields to int types. So when we read from file we need to convert the text back into int. So changed the readFile() method. Also when we write, we are writing ints, so we use %d instead of %s. Also the various setText() in playerComboActionPerformed( ) should use "" + <int_val> to set the corresponding values into the labels.

I have called the method to calcualate average. I am not sure of the formula though to calculate the batting average. Please make sure your formula is correct. You have not used battingAvg to display it anywhere in code. You can use getBattingAvg() method in the player class. I am giving the modified class and methods below. Hope it helps. If it helped, please do rate the answer.

player class

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

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 void avgCalc(){
   battingAVG = hits/(atBats - walks);
}
}

method readFile()

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

method writeFile()

public void writeFile(){
PrintWriter writePlayer = null;
player p;
try {
writePlayer = new PrintWriter("c:\Data\pirates_stats.csv");
for (int i = 0; i < newPlayer.size(); i++) {
   p = newPlayer.get(i);
writePlayer.printf("%s,%s,%d,%d,%d,%d,%d,%d ", p.getName(), p.getPosition(),
       p.getGames(),p.getAtBats(),p.getRuns(),p.getHits(),p.getWalks(),p.getStrikeOuts());
//%n is the windows /n
}
} catch (FileNotFoundException ex) {
JOptionPane.showMessageDialog(this, ex.getMessage());
System.exit(1);
}
writePlayer.close();
}

method baseballForm.playerComboActionPerformed()

private void playerComboActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String choice = (String) this.playerCombo.getSelectedItem();
  
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());
break;
}
}
}   

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