Java,分隔符

时间:2017-02-22 17:38:18

标签: java delimiter

我有这个代码用于分隔符:(":|\\s+")。 我想要允许这样的符号:(":"),但是如果我使用它,程序不起作用 - 它会在第一个问题之后停止。

谁能看到我哪里出错?

import java.util.Scanner;
public class Salary
{
    public static void main(String[] args)
    {
        int hour1, hour2, minute1, minute2, salary; 
        double totaly, totSalary, totHours, totMinutes, totDecMinutes;

        salary = 190;       

        Scanner scan = new Scanner(System.in);

        scan.useDelimiter(":|\\s+"); 

        System.out.println("Write the time when you startet working."
            + "Use the format (hh:mm).");

        hour1 = scan.nextInt();
        minute1 = scan.nextInt();

        System.out.println("Write the time when you stopped working."
            + "Use the formatet (hh:mm).");

        hour2 = scan.nextInt();
        minute2 = scan.nextInt();

        // Counts the number of workinghours.
        totHours = (hour2 - hour1);

        // Counts the number om workingminutes, decimal number
        totMinutes = (minute2 - minute1);
        totDecMinutes = totMinutes/60;

        // Counts the salary: total hours.
        totaly = totHours + totDecMinutes;
        totSalary = totaly*salary;

        System.out.printf("Din lön för idag blir: %.2f kr.", totSalary);
    }
}

1 个答案:

答案 0 :(得分:0)

我认为(不确定)换行符被解释为某种空间组合。上面使用的分隔符在或一个或多个空格上分隔。如果我写了 2:34 然后点击回车转到换行符,扫描仪会分隔:给出2小时,然后分隔换行符,分钟给出34。我不确定换行符如何转换为ASCII,并且无论如何它都随操作系统而变化。

这就是为什么我更喜欢使用 Scanner.nextLine()来返回行的其余部分的输入,不包括末尾的任何行分隔符。清理用户输入也更容易(参见下面的示例代码),因为如果上一次的格式不正确,我可以反复要求用户提供时间。

public class Salary
{
public static void main(String[] args)
{
    int hour1, hour2, minute1, minute2, salary; 
    double totaly, totSalary, totHours, totMinutes, totDecMinutes;

    salary = 190;       

    Scanner scan = new Scanner(System.in);

    System.out.print("Write the time when you startet working."
            + "Use the format (hh:mm).\n>> ");
    while (true) {
        String s = scan.nextLine();
        String[] parsed = s.split(":");
        if (s.split(":").length == 2) {
            try {
                hour1 = Integer.parseInt(parsed[0]);
                minute1 = Integer.parseInt(parsed[1]);
                break;
            } catch (NumberFormatException e) {
            }
        }
        System.out.print("Invalid input. Try again\n>> ");
    }

    System.out.print("Write the time when you stopped working."
            + "Use the formatet (hh:mm).\n>> ");
    while (true) {
        String s = scan.nextLine();
        String[] parsed = s.split(":");
        if (s.split(":").length == 2) {
            try {
                hour2 = Integer.parseInt(parsed[0]);
                minute2 = Integer.parseInt(parsed[1]);
                break;
            } catch (NumberFormatException e) {
            }
        }
        System.out.print("Invalid input. Try again\n>> ");
    }

    // Counts the number of workinghours.
    totHours = (hour2 - hour1);

    // Counts the number om workingminutes, decimal number
    totMinutes = (minute2 - minute1);
    totDecMinutes = totMinutes/60.0;

    // Counts the salary: total hours.
    totaly = totHours + totDecMinutes;
    totSalary = totaly*salary;

    System.out.printf("Din lön för idag blir: %.2f kr.", totSalary);
}

}