Reputation: 721
I am making a probabilistic outcome simulator Java Program. This program takes in a CSV file (or any document file) from the user in the FileChooser. The user will then hit the Run button, and the program will begin reading the CSV (our preferred file) file. Here's my code so far:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.layout.*;
import javafx.scene.control.*;
import javafx.stage.FileChooser;
import javafx.geometry.*;
import java.util.*;
import java.io.*;
public class POS extends Application
{
private Button runBtn = new Button("Run");
@Override
public void start(Stage stage)
{
GridPane pane = new GridPane();
VBox vBox = new VBox(20);
vBox.setPadding(new Insets(15));
Button selectBtn = new Button("Select File");
selectBtn.setStyle("-fx-font: 22 arial; -fx-base: #b6e7c9;");
vBox.getChildren().add(selectBtn);
selectBtn.setOnAction(e->
{
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Open Resource File");
File file = fileChooser.showOpenDialog(stage);
String regEx = "([^\\s]+(\\.(?i)(txt|doc|csv|pdf|xlsx))$)";
if (file.getName().matches(regEx))
{
run(file);
}
else
{
System.out.println("Please enter a valid CSV file");
}
});
RadioButton weekBtn = new RadioButton("Current Week");
RadioButton seasonBtn = new RadioButton("Entire Season");
runBtn.setStyle("-fx-font: 22 arial; -fx-base: #b6e7c9;");
seasonBtn.setDisable(true);
vBox.getChildren().add(weekBtn);
vBox.getChildren().add(seasonBtn);
vBox.getChildren().add(runBtn);
pane.add(vBox, 0, 0);
Scene scene = new Scene(pane, 500, 200);
stage.setScene(scene);
stage.setTitle("POS");
stage.show();
}
public void run(File file)
{
runBtn.setOnAction(e->
{
try
{
Scanner input = new Scanner(file);
sortFile(file, input);
input.nextLine();
input.close();
}
catch (InputMismatchException ex)
{
System.out.println("Error you seem to have typed the wrong type of file");
}
catch(IOException ex)
{
System.out.println("Error, file could not be found");
}
});
}
public ArrayList<String> sortFile(File file, Scanner input)
{
Random r = new Random();
input.next();
int homeRank = input.nextInt();
input.next();
input.next();
input.next();
input.next();
int roadRank = input.nextInt();
System.out.println("Home: " + homeRank + "road: " + roadRank);
int lowestTeamRank = Math.abs(homeRank - roadRank);
if (input.hasNext())
{
return null;
}
return null;
}
}
When I "the user", select a file that's, let's say, an invalid file, the program will tell me that this is invalid. If I select a .csv file called "NFLData", the program tells me "Error you seem to have typed the wrong type of file" (A MalformedException). If I select a .xlsx file with nothing in it, the program does not throw a malformed exception, rather it tells me that it is empty. How can I let my program accept my NFLData .csv file?
Upvotes: 2
Views: 134
Reputation: 721
I have realized my error is in the run block. Whenever I initiate input.next(), the Scanner takes in the whole line in the .csv separated by a comma delimiter.
Example: Jack, 22, John, 21, Henry, 24
I was creating incorrect new reference types with that reference pointing to the whole line of data in the .csv file.
Upvotes: 1