1

There is a file.txt that contains:

Country,Province/State,City

So I wanted to use "," as my delimiter. However, some lines of this file have:

Country,"Name1 and Name2, Province of",City

So using a comma as a delimiter would not work as the above example as a comma in the Province name.

This is my code:

String path = workingDir + "/src/main_pkg/File.txt";
    File file = new File(path);
    Scanner sc = null;
    try {
        sc = new Scanner(file);
    } catch (FileNotFoundException e) {
        System.out.println(new File("."));
        e.printStackTrace();
    }
    ObservableList<String> listOfProvinces = FXCollections.observableArrayList();
    String country;
    String state= "";
    String lastState = "";
    sc.useDelimiter(",");
    while (sc.hasNext()) {
        country = sc.next();
        province = sc.next();

        if (country.matches(countryComboBox.getValue())) {
            if (!province.matches(lastState)) {
                listOfStates.add(state);
                lastState = state;
            }
        }
        sc.nextLine();
    }
    sc.close();

To clarify, I cannot modify the txt file.

Dave Newton
  • 152,765
  • 23
  • 240
  • 286
  • 1
    The format of your file is "CSV" (comma-separated values), which allows double-quoted strings to contain comma's inside the values. Just search for a CSV-reading library for Java (Stackoverflow doesn't recommend libraries or external resources) – Erwin Bolwidt Nov 23 '19 at 02:39
  • A scanner is meant for simple tasks. You can try and find a delimiter from the text using tricks, but it is better to use regexp find. I was going to write an answer, but I think Erwin above nailed it, a CSV parser is what you really want. – Maarten Bodewes Nov 23 '19 at 02:45
  • Thanks @ErwinBolwidt (and Maarten) I did some searching with what you had said in mind and came across this post: https://stackoverflow.com/questions/1757065/java-splitting-a-comma-separated-string-but-ignoring-commas-in-quotes which had what I needed – notansandwich Nov 23 '19 at 03:18
  • 1
    Solutions should be posted as answers, not additions to the question. You’re still better off with an actual csv parser. – Dave Newton Nov 23 '19 at 03:30
  • Yes reading CSV is not hard but also fiddly to get right. For example quoted strings can contain doubled quotes that stand for a single one. Etc. You're _much_ better off using a tested library. – Gene Nov 23 '19 at 04:18
  • Thanks @DaveNewton I added my solution as an answer. – notansandwich Nov 23 '19 at 04:28

1 Answers1

0

I found the solution. I had to change my delimiter to:

sc.useDelimiter("(?!\"),(?!\")");

Which I got from: https://stackoverflow.com/a/1757146/12179513 The answer from Java: splitting a comma-separated string but ignoring commas in quotes works too, but it makes my program take a lot longer to run.