【问题标题】:Java, delimiterJava,分隔符
【发布时间】:2017-02-22 17:38:17
【问题描述】:

我有这个分隔符代码:(":|\\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);
    }
}

【问题讨论】:

    标签: java delimiter


    【解决方案1】:

    我认为(虽然不确定)换行符被解释为某种空格组合。上面使用的分隔符分隔 : 或一个或多个空格。如果我写了 2:34 然后单击 Enter 转到换行符,扫描仪将在 : 上定界,以 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);
    }
    

    }

    【讨论】:

    • 非常感谢。我会试试这个。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-23
    • 2014-07-29
    • 1970-01-01
    • 2014-04-03
    • 1970-01-01
    相关资源
    最近更新 更多