0

I specifically want to only use a comma as my delimiter to read a CSV file.

For instance, a program might read a person's name, clock-in, and clock-out times. The associated CSV file might contain:

John Doe, 09:00, 17:00

The program should be able to read three separate tokens:

"John Doe", "09:00", "17:00"

the code I have set up goes something like this:

/* main method */
    public static void main(String[] args) throws FileNotFoundException {
        // scanner object
        Scanner get = new Scanner(System.in);

        // set delimiter, read comma and single space as delimiter
        get.useDelimiter(", ");

        // prompt for file
        System.out.println("Provide a file name:");
        File employeeRecords = new File(get.nextLine());
        get = new Scanner(employeeRecords);

        // test run
        // read: employeeName, clockIn, lunchIn, lunchOut, clockOut
        // "Employee" is an object
        Employee $1 = new Employee(get.next(), get.next(), get.next(), get.next(), get.next());

        // .timeWorked is a method of the "Employee" object
        System.out.println("\nEmployee $1: " + $1.name + ", minutes worked: " + $1.timeWorked);
    }

However, the problem I get is whenever I provide the csv file, the scanner ignores the delimiter arguments I provided.

The csv file specifically only contains one line:

John, 07:59, 12:00, 12:31, 17:00

The tokens I get from the five get.next() statements are:

"John,", "07:59,", "12:00,", "12:31,", "17:00"

which means that the print statement prints out

Employee $1: John,, minutes worked: -1443

Ignoring the obvious bug in minutes worked, is there a way to only use commas as the delimiter?

EDIT: Not sure how to mark a comment as the answer, or if it is possible, but my bug in question was my delimiter had been defined before redefining the scanner object, as Slaw points out.

Xbox 360
  • 1
  • 1
  • 1
  • 2
    You call `get.useDelimiter(", ")` but then create a **new** `Scanner`. You don't set the delimiter of this new `Scanner` to a non-default value. You should probably just move `get.useDelimiter(", ")` to after `get = new Scanner(employeeRecords)`. – Slaw Jul 08 '22 at 22:22
  • What if their name is John Doe, Jr.? – David Conrad Jul 08 '22 at 22:25
  • Delete `get.useDelimiter(", ");` and add it to the second assignment: `get = new Scanner(employeeRecords).useDelimiter(", ");` – Bohemian Jul 08 '22 at 22:29
  • [This Question](https://stackoverflow.com/questions/28766377/how-do-i-use-a-delimiter-with-scanner-usedelimiter-in-java) is about the `Scanner` and `useDelimiter`. The answer is quite detailed, maybe you want to have a look at it. – Kayz Jul 08 '22 at 22:36
  • @Slaw Yeah you provided the solution to the specific issue I'm looking to solve. I did in fact redefine the scanner object after adjusting the delimiter. – Xbox 360 Jul 08 '22 at 23:16

1 Answers1

0

Two options:

  • delete the comma manually (as the problem appears to happen at the last character only)
  • read in entire lines (call that line string) and use string.split(","), instead of using the delimiter in the scanner class itself. This will return a string array containing the line, split by commas.
Leaderboard
  • 371
  • 1
  • 10