My coding compile won\'t run, can anyone figure that out for me? Thank you. Base
ID: 3751600 • Letter: M
Question
My coding compile won't run, can anyone figure that out for me? Thank you.
BaseballA.java coding here:
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import javafx.event.ActionEvent;
import javafx.scene.Node;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import javafx.stage.FileChooser.ExtensionFilter;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
public class BaseballA extends javafx.application.Application implements javafx.event.EventHandler<ActionEvent>
{
Stage stage;
javafx.scene.Scene scene;
public BaseballA() {}
private TextField tfFileName = new TextField();
private Button btnOpen = new Button("Open");
private TextField tfRecordsIn = new TextField();
private TextField tfRecordsOut = new TextField();
private TextArea taData = new TextArea();
public static void main(String[] args) {
launch(args);
}
public void start(Stage _stage)
{
stage = _stage;
stage.setTitle("Baseball");
stage.setOnCloseRequest(
new javafx.event.EventHandler(){
public void handle(WindowEvent evt) {
System.exit(0);
}
});
VBox root = new VBox(8.0D);
tfFileName.setPrefColumnCount(20);
tfRecordsIn.setPrefColumnCount(5);
tfRecordsOut.setPrefColumnCount(5);
taData.setPrefHeight(500.0D);
FlowPane fpRow1 = new FlowPane(8.0D, 8.0D);
fpRow1.getChildren().addAll(new Node[] { new Label("File: "), tfFileName, btnOpen });
root.getChildren().add(fpRow1);
FlowPane fpRow2 = new FlowPane(8.0D, 8.0D);
fpRow2.getChildren().addAll(new Node[] { new Label("Records In: "), tfRecordsIn, new Label("Records Out: "), tfRecordsOut });
root.getChildren().add(fpRow2);
root.getChildren().add(taData);
btnOpen.setOnAction(this);
taData.setFont(javafx.scene.text.Font.font("MONOSPACED", 12.0D));
tfFileName.setDisable(true);
scene = new javafx.scene.Scene(root, 375.0D, 400.0D);
stage.setScene(scene);
stage.show();
}
public void handle(ActionEvent ae)
{
FileChooser chooser = new FileChooser();
chooser.setInitialDirectory(new File("."));
chooser.setTitle("Select CSV file");
chooser.getExtensionFilters().addAll(new FileChooser.ExtensionFilter[] { new FileChooser.ExtensionFilter("CSV files", new String[] { "*.csv" }), new FileChooser.ExtensionFilter("All Files", new String[] { "*.*" }) });
File csvFile = chooser.showOpenDialog(stage);
if (csvFile == null) {
return;
}
tfFileName.setText(csvFile.getName());
File datFile = null;
int index = csvFile.getName().lastIndexOf('.');
if ((index > 0) && (index <= csvFile.getName().length() - 2)) {
datFile = new File(csvFile.getName().substring(0, index) + ".dat");
}
taData.setText("");
BaseballA.TwoCounts results = convertCSVtoDAT(csvFile, datFile);
tfRecordsIn.setText("" + results.getRecordsIn());
tfRecordsOut.setText("" + results.getRecordsOut());
readDat(datFile);
}
class TwoCounts {
private int recordsIn;
private int recordsOut;
public TwoCounts(int in, int out) {
recordsIn = in;recordsOut = out; }
public int getRecordsIn() {
return recordsIn; }
public int getRecordsOut() {
return recordsOut; }
}
private BaseballA.TwoCounts convertCSVtoDAT(File csvFile, File datFile)
{
DataOutputStream dos = null;
BufferedReader br = null;
int inCount = 0;
int outCount = 0;
String errorMsg = "";
String errorItem = "???";
try
{
br = new BufferedReader(new java.io.FileReader(csvFile));
dos = new DataOutputStream(new java.io.BufferedOutputStream(new java.io.FileOutputStream(datFile)));
br.readLine();
inCount++;
String line;
while ((line = br.readLine()) != null) {
inCount++;
errorMsg = "Error found in: " + line + " ";
if (line.trim().length() != 0)
{
String[] ary = line.split(",");
try
{
String nameF = ary[0].trim();
String nameL = ary[1].trim();
errorItem = "Birth day";
int bDay = Integer.parseInt(ary[2]);
errorItem = "Birth month";
int bMon = Integer.parseInt(ary[3]);
errorItem = "Birth year";
int bYear = Integer.parseInt(ary[4]);
errorItem = "Weight";
int weight = Integer.parseInt(ary[5]);
errorItem = "Height";
double height = Double.parseDouble(ary[6]);
}
catch (NumberFormatException nfe) {
double height;
taData.appendText(errorMsg + " Offending item is: " + errorItem + " "); }
continue;
double height;
int weight;
int bYear;
int bMon; int bDay; String nameL; String nameF; dos.writeUTF(nameF);
dos.writeUTF(nameL);
dos.writeInt(bDay);
dos.writeInt(bMon);
dos.writeInt(bYear);
dos.writeInt(weight);
dos.writeDouble(height);
outCount++;
}
}
dos.flush();
dos.close();
br.close();
return new BaseballA.TwoCounts(inCount, outCount);
}
catch (FileNotFoundException fnfe) {
Alert alert = new Alert(javafx.scene.control.Alert.AlertType.ERROR, "File " + csvFile.getName() + " does not exist. Exiting.", new ButtonType[0]);
alert.setHeaderText("No File");
alert.showAndWait();
System.exit(2);
}
catch (IOException ioe) {
Alert alert = new Alert(javafx.scene.control.Alert.AlertType.ERROR, "" + ioe, new ButtonType[0]);
alert.setHeaderText("IO Exception");
alert.showAndWait();
System.exit(3);
}
return new BaseballA.TwoCounts(0, 0);
}
private void readDat(File datFile)
{
try {
DataInputStream dis = new DataInputStream(new java.io.BufferedInputStream(new java.io.FileInputStream(datFile)));
taData.appendText(String.format(" %-20s %10s %7s %7s ", new Object[] { "First & Last name", "Birthdate", "Weight", "Height" }));
try
{
for (;;)
{
String nameF = dis.readUTF();
String nameL = dis.readUTF();
int bDay = dis.readInt();
int bMon = dis.readInt();
int bYear = dis.readInt();
int weight = dis.readInt();
double height = dis.readDouble();
taData.appendText(String.format("%-20s %10s %7s %7s ", new Object[] { nameF + " " + nameL, bMon + "/" + bDay + "/" + bYear,
Integer.valueOf(weight), Double.valueOf(height) }));
}
}
catch (java.io.EOFException eof)
{
dis.close();
}
return;
}
catch (FileNotFoundException fnfe)
{
System.err.println("File " + datFile.getName() + " does not exist. Exiting.");
System.exit(2);
}
catch (IOException ioe) {
System.err.println("Some IO error.");
ioe.printStackTrace();
}
}
}
BaseballB.java coding here:
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.scene.Node;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import javafx.stage.FileChooser.ExtensionFilter;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
public class BaseballB extends javafx.application.Application implements javafx.event.EventHandler<ActionEvent>
{
Stage stage;
javafx.scene.Scene scene;
public BaseballB() {}
private TextField tfFileName = new TextField();
private Button btnOpen = new Button("Open");
private TextField tfRecordsIn = new TextField();
private TextField tfRecordsOut = new TextField();
private TextArea taData = new TextArea();
private boolean errorFlag = false;
private String errorItem = "";
public static void main(String[] args) {
launch(args);
}
public void start(Stage _stage)
{
stage = _stage;
stage.setTitle("Baseball - P. Lutz");
stage.setOnCloseRequest(new javafx.event.EventHandler() {
public void handle(WindowEvent evt) {
System.exit(0);
}
});
VBox root = new VBox(8.0D);
tfFileName.setPrefColumnCount(20);
tfRecordsIn.setPrefColumnCount(5);
tfRecordsOut.setPrefColumnCount(5);
taData.setPrefHeight(500.0D);
FlowPane fpRow1 = new FlowPane(8.0D, 8.0D);
fpRow1.getChildren().addAll(new Node[] { new Label("File: "), tfFileName, btnOpen });
root.getChildren().add(fpRow1);
FlowPane fpRow2 = new FlowPane(8.0D, 8.0D);
fpRow2.getChildren().addAll(new Node[] { new Label("Records In: "), tfRecordsIn, new Label("Records Out: "), tfRecordsOut });
root.getChildren().add(fpRow2);
root.getChildren().add(taData);
btnOpen.setOnAction(this);
taData.setFont(javafx.scene.text.Font.font("MONOSPACED", 12.0D));
tfFileName.setDisable(true);
scene = new javafx.scene.Scene(root, 375.0D, 400.0D);
stage.setScene(scene);
stage.show();
}
public void handle(ActionEvent ae)
{
FileChooser chooser = new FileChooser();
chooser.setInitialDirectory(new File("."));
chooser.setTitle("Select CSV file");
chooser.getExtensionFilters().addAll(new FileChooser.ExtensionFilter[] { new FileChooser.ExtensionFilter("CSV files", new String[] { "*.csv" }), new FileChooser.ExtensionFilter("All Files", new String[] { "*.*" }) });
File csvFile = chooser.showOpenDialog(stage);
if (csvFile == null) { return;
}
tfFileName.setText(csvFile.getName());
File datFile = null;
int index = csvFile.getName().lastIndexOf('.');
if ((index > 0) && (index <= csvFile.getName().length() - 2)) {
datFile = new File(csvFile.getName().substring(0, index) + ".dat");
}
taData.setText("");
BaseballB.TwoCounts results = convertCSVtoDAT(csvFile, datFile);
tfRecordsIn.setText("" + results.getRecordsIn());
tfRecordsOut.setText("" + results.getRecordsOut());
readDat(datFile);
}
class TwoCounts {
private int recordsIn;
private int recordsOut;
public TwoCounts(int in, int out) {
recordsIn = in;recordsOut = out; }
public int getRecordsIn() { return recordsIn; }
public int getRecordsOut() { return recordsOut; }
}
private BaseballB.TwoCounts convertCSVtoDAT(File csvFile, File datFile)
{
DataOutputStream dos = null;
BufferedReader br = null;
int inCount = 0;
int outCount = 0;
String errorMsg = "";
String errorItem = "???";
try
{
br = new BufferedReader(new java.io.FileReader(csvFile));
dos = new DataOutputStream(new java.io.BufferedOutputStream(new java.io.FileOutputStream(datFile)));
br.readLine();
inCount++;
String line;
while ((line = br.readLine()) != null) {
inCount++;
errorMsg = "Error found in: " + line + " ";
errorFlag = false;
errorItem = "";
if (line.trim().length() != 0)
{
String[] ary = line.split(",");
if (ary.length != 7) {
errorFlag = true;
taData.appendText(errorMsg + "Not enough fields in the record. ");
}
else
{
String nameF = ary[0].trim();
String nameL = ary[1].trim();
int bDay = intCvt(ary[2], "Birth day");
int bMon = intCvt(ary[3], "Birth month");
int bYear = intCvt(ary[4], "Birth year");
int weight = intCvt(ary[5], "Weight");
double height = dblCvt(ary[6], "Height");
if (errorFlag) {
taData.appendText(errorMsg + "Offending data: " + errorItem + " ");
}
else
{
dos.writeUTF(nameF);
dos.writeUTF(nameL);
dos.writeInt(bDay);
dos.writeInt(bMon);
dos.writeInt(bYear);
dos.writeInt(weight);
dos.writeDouble(height);
outCount++;
}
}
}
}
dos.flush();
dos.close();
br.close();
return new BaseballB.TwoCounts(inCount, outCount);
}
catch (FileNotFoundException fnfe) {
Alert alert = new Alert(javafx.scene.control.Alert.AlertType.ERROR, "File " + csvFile.getName() + " does not exist. Exiting.", new ButtonType[0]);
alert.setHeaderText("No File");
alert.showAndWait();
System.exit(2);
}
catch (IOException ioe) {
Alert alert = new Alert(javafx.scene.control.Alert.AlertType.ERROR, "" + ioe, new ButtonType[0]);
alert.setHeaderText("IO Exception");
alert.showAndWait();
System.exit(3);
}
return new BaseballB.TwoCounts(0, 0);
}
private void readDat(File datFile)
{
try {
DataInputStream dis = new DataInputStream(new java.io.BufferedInputStream(new java.io.FileInputStream(datFile)));
taData.appendText(String.format(" %-20s %10s %7s %7s ", new Object[] { "First & Last name", "Birthdate", "Weight", "Height" }));
try
{
for (;;)
{
String nameF = dis.readUTF();
String nameL = dis.readUTF();
int bDay = dis.readInt();
int bMon = dis.readInt();
int bYear = dis.readInt();
int weight = dis.readInt();
double height = dis.readDouble();
taData.appendText(String.format("%-20s %10s %7s %7s ", new Object[] { nameF + " " + nameL, bMon + "/" + bDay + "/" + bYear,
Integer.valueOf(weight), Double.valueOf(height) }));
}
}
catch (java.io.EOFException eof)
{
dis.close();
}
return;
}
catch (FileNotFoundException fnfe)
{
System.err.println("File " + datFile.getName() + " does not exist. Exiting.");
System.exit(2);
}
catch (IOException ioe) {
System.err.println("Some IO error.");
ioe.printStackTrace();
}
}
private int intCvt(String item, String errTxt)
{
try
{
return Integer.parseInt(item);
}
catch (NumberFormatException nfe) {
if (errorFlag) {
errorItem += ", ";
}
errorItem += errTxt;
errorFlag = true;
}
return -1;
}
private double dblCvt(String item, String errTxt)
{
try
{
return Double.parseDouble(item);
}
catch (NumberFormatException nfe) {
if (errorFlag) {
errorItem += ", ";
}
errorItem += errTxt;
errorFlag = true;
}
return -1.0D;
}
}
Explanation / Answer
Please find the error-free code below.
CODE
====================
BaseballA.java
******************************
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import javafx.scene.Node;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import javafx.stage.FileChooser.ExtensionFilter;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import javafx.application.*;
import javafx.event.*;
public class BaseballA extends javafx.application.Application implements javafx.event.EventHandler<ActionEvent> {
Stage stage;
javafx.scene.Scene scene;
public BaseballA() {}
private TextField tfFileName = new TextField();
private Button btnOpen = new Button("Open");
private TextField tfRecordsIn = new TextField();
private TextField tfRecordsOut = new TextField();
private TextArea taData = new TextArea();
public static void main(String[] args) {
launch(args);
}
public void start(Stage _stage)
{
stage = _stage;
stage.setTitle("Baseball");
stage.setOnCloseRequest(new EventHandler<WindowEvent>(){
@Override
public void handle(WindowEvent event) {
System.exit(0);
}
});
VBox root = new VBox(8.0D);
tfFileName.setPrefColumnCount(20);
tfRecordsIn.setPrefColumnCount(5);
tfRecordsOut.setPrefColumnCount(5);
taData.setPrefHeight(500.0D);
FlowPane fpRow1 = new FlowPane(8.0D, 8.0D);
fpRow1.getChildren().addAll(new Node[] { new Label("File: "), tfFileName, btnOpen });
root.getChildren().add(fpRow1);
FlowPane fpRow2 = new FlowPane(8.0D, 8.0D);
fpRow2.getChildren().addAll(new Node[] { new Label("Records In: "), tfRecordsIn, new Label("Records Out: "), tfRecordsOut });
root.getChildren().add(fpRow2);
root.getChildren().add(taData);
btnOpen.setOnAction(this);
taData.setFont(javafx.scene.text.Font.font("MONOSPACED", 12.0D));
tfFileName.setDisable(true);
scene = new javafx.scene.Scene(root, 375.0D, 400.0D);
stage.setScene(scene);
stage.show();
}
public void handle(ActionEvent ae)
{
FileChooser chooser = new FileChooser();
chooser.setInitialDirectory(new File("."));
chooser.setTitle("Select CSV file");
chooser.getExtensionFilters().addAll(new FileChooser.ExtensionFilter[] { new FileChooser.ExtensionFilter("CSV files", new String[] { "*.csv" }), new FileChooser.ExtensionFilter("All Files", new String[] { "*.*" }) });
File csvFile = chooser.showOpenDialog(stage);
if (csvFile == null) {
return;
}
tfFileName.setText(csvFile.getName());
File datFile = null;
int index = csvFile.getName().lastIndexOf('.');
if ((index > 0) && (index <= csvFile.getName().length() - 2)) {
datFile = new File(csvFile.getName().substring(0, index) + ".dat");
}
taData.setText("");
BaseballA.TwoCounts results = convertCSVtoDAT(csvFile, datFile);
tfRecordsIn.setText("" + results.getRecordsIn());
tfRecordsOut.setText("" + results.getRecordsOut());
readDat(datFile);
}
class TwoCounts {
private int recordsIn;
private int recordsOut;
public TwoCounts(int in, int out) {
recordsIn = in;recordsOut = out; }
public int getRecordsIn() {
return recordsIn; }
public int getRecordsOut() {
return recordsOut; }
}
private BaseballA.TwoCounts convertCSVtoDAT(File csvFile, File datFile)
{
DataOutputStream dos = null;
BufferedReader br = null;
int inCount = 0;
int outCount = 0;
String errorMsg = "";
String errorItem = "???";
try
{
br = new BufferedReader(new java.io.FileReader(csvFile));
dos = new DataOutputStream(new java.io.BufferedOutputStream(new java.io.FileOutputStream(datFile)));
br.readLine();
inCount++;
String line = "";
double height = 0;
int weight = 0;
int bYear = 0;
int bMon = 0;
int bDay = 0;
String nameL = "";
String nameF = "";
while ((line = br.readLine()) != null) {
inCount++;
errorMsg = "Error found in: " + line + " ";
if (line.trim().length() != 0)
{
String[] ary = line.split(",");
try
{
nameF = ary[0].trim();
nameL = ary[1].trim();
errorItem = "Birth day";
bDay = Integer.parseInt(ary[2]);
errorItem = "Birth month";
bMon = Integer.parseInt(ary[3]);
errorItem = "Birth year";
bYear = Integer.parseInt(ary[4]);
errorItem = "Weight";
weight = Integer.parseInt(ary[5]);
errorItem = "Height";
height = Double.parseDouble(ary[6]);
}
catch (NumberFormatException nfe) {
taData.appendText(errorMsg + " Offending item is: " + errorItem + " "); }
continue;
}
dos.writeUTF(nameF);
dos.writeUTF(nameL);
dos.writeInt(bDay);
dos.writeInt(bMon);
dos.writeInt(bYear);
dos.writeInt(weight);
dos.writeDouble(height);
outCount++;
}
dos.flush();
dos.close();
br.close();
return new BaseballA.TwoCounts(inCount, outCount);
}
catch (FileNotFoundException fnfe) {
Alert alert = new Alert(javafx.scene.control.Alert.AlertType.ERROR, "File " + csvFile.getName() + " does not exist. Exiting.", new ButtonType[0]);
alert.setHeaderText("No File");
alert.showAndWait();
System.exit(2);
}
catch (IOException ioe) {
Alert alert = new Alert(javafx.scene.control.Alert.AlertType.ERROR, "" + ioe, new ButtonType[0]);
alert.setHeaderText("IO Exception");
alert.showAndWait();
System.exit(3);
}
return new BaseballA.TwoCounts(0, 0);
}
private void readDat(File datFile)
{
try {
DataInputStream dis = new DataInputStream(new java.io.BufferedInputStream(new java.io.FileInputStream(datFile)));
taData.appendText(String.format(" %-20s %10s %7s %7s ", new Object[] { "First & Last name", "Birthdate", "Weight", "Height" }));
try
{
for (;;)
{
String nameF = dis.readUTF();
String nameL = dis.readUTF();
int bDay = dis.readInt();
int bMon = dis.readInt();
int bYear = dis.readInt();
int weight = dis.readInt();
double height = dis.readDouble();
taData.appendText(String.format("%-20s %10s %7s %7s ", new Object[] { nameF + " " + nameL, bMon + "/" + bDay + "/" + bYear,
Integer.valueOf(weight), Double.valueOf(height) }));
}
}
catch (java.io.EOFException eof)
{
dis.close();
}
return;
}
catch (FileNotFoundException fnfe)
{
System.err.println("File " + datFile.getName() + " does not exist. Exiting.");
System.exit(2);
}
catch (IOException ioe) {
System.err.println("Some IO error.");
ioe.printStackTrace();
}
}
}
BaseballB.java
******************************
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.scene.Node;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import javafx.stage.FileChooser.ExtensionFilter;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import javafx.event.*;
public class BaseballB extends javafx.application.Application implements javafx.event.EventHandler<ActionEvent>
{
Stage stage;
javafx.scene.Scene scene;
public BaseballB() {}
private TextField tfFileName = new TextField();
private Button btnOpen = new Button("Open");
private TextField tfRecordsIn = new TextField();
private TextField tfRecordsOut = new TextField();
private TextArea taData = new TextArea();
private boolean errorFlag = false;
private String errorItem = "";
public static void main(String[] args) {
launch(args);
}
public void start(Stage _stage)
{
stage = _stage;
stage.setTitle("Baseball - P. Lutz");
stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
public void handle(WindowEvent evt) {
System.exit(0);
}
});
VBox root = new VBox(8.0D);
tfFileName.setPrefColumnCount(20);
tfRecordsIn.setPrefColumnCount(5);
tfRecordsOut.setPrefColumnCount(5);
taData.setPrefHeight(500.0D);
FlowPane fpRow1 = new FlowPane(8.0D, 8.0D);
fpRow1.getChildren().addAll(new Node[] { new Label("File: "), tfFileName, btnOpen });
root.getChildren().add(fpRow1);
FlowPane fpRow2 = new FlowPane(8.0D, 8.0D);
fpRow2.getChildren().addAll(new Node[] { new Label("Records In: "), tfRecordsIn, new Label("Records Out: "), tfRecordsOut });
root.getChildren().add(fpRow2);
root.getChildren().add(taData);
btnOpen.setOnAction(this);
taData.setFont(javafx.scene.text.Font.font("MONOSPACED", 12.0D));
tfFileName.setDisable(true);
scene = new javafx.scene.Scene(root, 375.0D, 400.0D);
stage.setScene(scene);
stage.show();
}
public void handle(ActionEvent ae)
{
FileChooser chooser = new FileChooser();
chooser.setInitialDirectory(new File("."));
chooser.setTitle("Select CSV file");
chooser.getExtensionFilters().addAll(new FileChooser.ExtensionFilter[] { new FileChooser.ExtensionFilter("CSV files", new String[] { "*.csv" }), new FileChooser.ExtensionFilter("All Files", new String[] { "*.*" }) });
File csvFile = chooser.showOpenDialog(stage);
if (csvFile == null) { return;
}
tfFileName.setText(csvFile.getName());
File datFile = null;
int index = csvFile.getName().lastIndexOf('.');
if ((index > 0) && (index <= csvFile.getName().length() - 2)) {
datFile = new File(csvFile.getName().substring(0, index) + ".dat");
}
taData.setText("");
BaseballB.TwoCounts results = convertCSVtoDAT(csvFile, datFile);
tfRecordsIn.setText("" + results.getRecordsIn());
tfRecordsOut.setText("" + results.getRecordsOut());
readDat(datFile);
}
class TwoCounts {
private int recordsIn;
private int recordsOut;
public TwoCounts(int in, int out) {
recordsIn = in;recordsOut = out; }
public int getRecordsIn() { return recordsIn; }
public int getRecordsOut() { return recordsOut; }
}
private BaseballB.TwoCounts convertCSVtoDAT(File csvFile, File datFile)
{
DataOutputStream dos = null;
BufferedReader br = null;
int inCount = 0;
int outCount = 0;
String errorMsg = "";
String errorItem = "???";
try
{
br = new BufferedReader(new java.io.FileReader(csvFile));
dos = new DataOutputStream(new java.io.BufferedOutputStream(new java.io.FileOutputStream(datFile)));
br.readLine();
inCount++;
String line;
while ((line = br.readLine()) != null) {
inCount++;
errorMsg = "Error found in: " + line + " ";
errorFlag = false;
errorItem = "";
if (line.trim().length() != 0)
{
String[] ary = line.split(",");
if (ary.length != 7) {
errorFlag = true;
taData.appendText(errorMsg + "Not enough fields in the record. ");
}
else
{
String nameF = ary[0].trim();
String nameL = ary[1].trim();
int bDay = intCvt(ary[2], "Birth day");
int bMon = intCvt(ary[3], "Birth month");
int bYear = intCvt(ary[4], "Birth year");
int weight = intCvt(ary[5], "Weight");
double height = dblCvt(ary[6], "Height");
if (errorFlag) {
taData.appendText(errorMsg + "Offending data: " + errorItem + " ");
}
else
{
dos.writeUTF(nameF);
dos.writeUTF(nameL);
dos.writeInt(bDay);
dos.writeInt(bMon);
dos.writeInt(bYear);
dos.writeInt(weight);
dos.writeDouble(height);
outCount++;
}
}
}
}
dos.flush();
dos.close();
br.close();
return new BaseballB.TwoCounts(inCount, outCount);
}
catch (FileNotFoundException fnfe) {
Alert alert = new Alert(javafx.scene.control.Alert.AlertType.ERROR, "File " + csvFile.getName() + " does not exist. Exiting.", new ButtonType[0]);
alert.setHeaderText("No File");
alert.showAndWait();
System.exit(2);
}
catch (IOException ioe) {
Alert alert = new Alert(javafx.scene.control.Alert.AlertType.ERROR, "" + ioe, new ButtonType[0]);
alert.setHeaderText("IO Exception");
alert.showAndWait();
System.exit(3);
}
return new BaseballB.TwoCounts(0, 0);
}
private void readDat(File datFile)
{
try {
DataInputStream dis = new DataInputStream(new java.io.BufferedInputStream(new java.io.FileInputStream(datFile)));
taData.appendText(String.format(" %-20s %10s %7s %7s ", new Object[] { "First & Last name", "Birthdate", "Weight", "Height" }));
try
{
for (;;)
{
String nameF = dis.readUTF();
String nameL = dis.readUTF();
int bDay = dis.readInt();
int bMon = dis.readInt();
int bYear = dis.readInt();
int weight = dis.readInt();
double height = dis.readDouble();
taData.appendText(String.format("%-20s %10s %7s %7s ", new Object[] { nameF + " " + nameL, bMon + "/" + bDay + "/" + bYear,
Integer.valueOf(weight), Double.valueOf(height) }));
}
}
catch (java.io.EOFException eof)
{
dis.close();
}
return;
}
catch (FileNotFoundException fnfe)
{
System.err.println("File " + datFile.getName() + " does not exist. Exiting.");
System.exit(2);
}
catch (IOException ioe) {
System.err.println("Some IO error.");
ioe.printStackTrace();
}
}
private int intCvt(String item, String errTxt)
{
try
{
return Integer.parseInt(item);
}
catch (NumberFormatException nfe) {
if (errorFlag) {
errorItem += ", ";
}
errorItem += errTxt;
errorFlag = true;
}
return -1;
}
private double dblCvt(String item, String errTxt)
{
try
{
return Double.parseDouble(item);
}
catch (NumberFormatException nfe) {
if (errorFlag) {
errorItem += ", ";
}
errorItem += errTxt;
errorFlag = true;
}
return -1.0D;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.