为什么我从 csv 文件中读取的最后一个单元格在我用作设置为变量的值的最后一个字符串的开头添加一个逗号?

Why does the last cell I read from a csv file add a comma at the beginning of the last string I'm using as a value to set to a variable?

提问人:SpyderCoder 提问时间:3/9/2021 最后编辑:javadevSpyderCoder 更新时间:3/9/2021 访问量:44

问:

private void populateEmployees() throws FileNotFoundException
    {
        File loc = new File(employeesPath);
        Scanner read = new Scanner(loc).useDelimiter(",");

        while (read.hasNext())
        {
            String firstName = read.next();
            String lastName = read.next();
            String age = read.next();
            String gender = read.next();
            String startDate = read.next();
            String currentEmployee = read.next();
            int totalHours = read.nextInt();
            String hourlyPay = read.nextLine();
            hourlyPay = hourlyPay.substring(1,hourlyPay.length() - 1);

            Employee employee = new Employee(firstName, lastName, age, gender, startDate, currentEmployee, totalHours, Double.parseDouble(hourlyPay));
            employees.add(employee);
        }

        read.close();
    }

虽然我确实找到了解决方案,但我仍然对为什么 String hourlyPay 在其字符串的开头添加逗号感到困惑。有人愿意对此提供一些见解吗?

爪哇岛 CSV的 解析 发短信 java.util.scanner

评论

0赞 Henry Twist 3/9/2021
你能包括你正在阅读的文件吗?

答: 暂无答案