如果声明没有正确“阅读”?

时间:2014-11-27 14:41:16

标签: java if-statement

我正在创建一个将标准时间转换为24小时制的程序。对我来说不幸的是,我的" if"用户输入错误时间的语句似乎无法正常工作。

这是我的代码(我已经包含了所有代码,因为我觉得这个错误可能是由另一条较低的行引起的):

public class StandardTime {

    static String amPM;//will hold am/pm for standard time 
    static String traditionalTime;//will store traditional time from user
    static int hours;//will store hours and minutes
    static int min1, min2;

    public static void main(String args[]) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));// user input

        int tryAgain = 1;//will initial do-while loop
        System.out.println("Standard Time to Traditional Time Converter");
        System.out.println("===========================================");
        do {
            System.out.println();
            System.out.println("Input a time in Standard Form (HH:MM:SS):");
            String standardTime = br.readLine();//user inputs time in standard form
            System.out.println();
            String minute1 = standardTime.substring(3);
            min1 = Integer.parseInt(minute1);
            String minute2 = standardTime.substring(5);
            min2 = Integer.parseInt(minute2);
            do {
                if (standardTime.length() != 8) {
                    System.out.println("Invalid time entered.");
                    System.out.println("Input a time in Standard Form that has this form HH:MM:SS ...");
                    standardTime = br.readLine();//user inputs time in standard form
                    System.out.println();
                }
                else if (min1 >= 6)
                {
                    System.out.println("Invalid time entered. Please do not input a minute time over 59.");
                    System.out.println("Input a time in Standard Form that has this form HH:MM:SS ...");
                    standardTime = br.readLine();//user inputs time in standard form
                    System.out.println();
                }
            } while ((standardTime.length()) != 8 || (min1 >= 6));
            //method declaration
            convertToTraditional(standardTime); // call the coversion method
            System.out.println(standardTime + " is equivalent to " + hours + ":" +min1+min2+ " " + amPM + ".");
            System.out.println();
            System.out.println("Enter 1 to try again or any other number to exit the program.");
            tryAgain = Integer.parseInt(br.readLine());//user decides to try again
        } while (tryAgain == 1);//will repeat if user enters 1

    }//closes main body

    public static void convertToTraditional(String standardTime) {
        String hour = standardTime.substring(0, 2); //captures substring of hours in user's input
        hours = Integer.parseInt(hour); //converts substring to INT value
        int pmHour = hours;
        if (12 == hours) { //creates a statement for 12PM (noon)
            hours = pmHour;
            amPM = "PM";
        } else if (hours > 12 && hours <= 23) { //if the time is not equal to noon or midnight
            hours = hours - 12;
            amPM = "PM";
        } else if (hours == 0) { //12 midnight
            hours = hours + 12;
            amPM = "AM";
        } else if (hours == 24) { //also another way to express 12 midnight on the 24 hour clock
            hours = hours - 12;
            amPM = "AM";
        } else { //else if the hours are between midnight and 11am
            hours = pmHour;
            amPM = "AM";
        }
    }
}

错误特别出现在if&#34; standardTime.length()!= 8。&#34;我希望用户使用以下格式键入它&#34; HH:MM:SS&#34;。这是我收到的错误,例如,我输入1:2:3 ......

Exception in thread "main" java.lang.NumberFormatException: For input string: ":3"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:492)
    at java.lang.Integer.parseInt(Integer.java:527)

    at standardtime.StandardTime.main(StandardTime.java:35)
Java Result: 1

感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

当您尝试将“:3”解析为数字,当它有冒号时,会出现错误,然后它不是数字而且会抛出此异常......

为什么不尝试使用java.util.Date?如果您使用它,您的代码将更清晰,更容易编写!

答案 1 :(得分:0)

你的问题不是提到的if语句,而是这段代码:

String standardTime = br.readLine();//user inputs time in standard form
System.out.println();
String minute1 = standardTime.substring(3);
min1 = Integer.parseInt(minute1);
String minute2 = standardTime.substring(5);
min2 = Integer.parseInt(minute2);

您正在阅读用户的输入,并且您立即使用它,因此之前检查输入的长度。

在上述行之间移动do/while循环以修复:

String standardTime = br.readLine();//user inputs time in standard form

do {
    // check
} while ((standardTime.length()) != 8 || (min1 >= 6));

System.out.println();
String minute1 = standardTime.substring(3);
min1 = Integer.parseInt(minute1);
String minute2 = standardTime.substring(5);
min2 = Integer.parseInt(minute2);