2

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?

Bytes
  • 691
  • 1
  • 7
  • 22
  • `String regEx = "([^\\s]+(\\.(?i)(txt|doc|csv|pdf|xlsx))$)";` I am guessing that this regex apples only to files' suffix. Be aware that `matches` method from String class checks if entire string can be matched by regex in one go. So ` if (file.getName().matches(regEx))` is not valid code if you want to test only suffix. Try maybe starting your regex with `.*` to allow it to match any characters before suffix. – Pshemo Dec 05 '15 at 01:29
  • Anyway `FileChooser` should have options to allow you to pick only files with specified extensions/suffixes. So maybe this will be better strategy for you: http://stackoverflow.com/questions/13634576/javafx-filechooser-how-to-set-file-filters – Pshemo Dec 05 '15 at 01:34
  • @Pshemo The error occurs only in the run method, when I create a Scanner. The catch method catches the error, and reports it as an inputmismatch exception – Bytes Dec 05 '15 at 01:51
  • 1
    @Pshemo I realize my error now, when I initiate input.next(), the Scanner is reading the whole line and is partitioned by (",") delimiters – Bytes Dec 05 '15 at 02:01

1 Answers1

1

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.

Bytes
  • 691
  • 1
  • 7
  • 22