0

I would like to have a validation for: number input only, hour must be <=24, minutes must be <60, and the user must type the ':' sign between hh:mm.

 int total; //Total Minutes
    String time; //Input from keyboard


    final Scanner T = new Scanner(System.in);

    System.out.print("Enter the time in HH:MM :");
    time = T.next();

    //Get the value before the ':'
    int hour = Integer.parseInt(time.substring(0, time.indexOf(':')));


    //Get the value after the ':'
    int minute =Integer.parseInt (time.substring((time.indexOf(':') + 1), time.length()));


    //Formula of the calculation
    total = hour * 60 + minute;


    //Display the final value
    System.out.println( time +" is " + total + " minutes.");
3
  • Possible duplicate of Regular expression for matching HH:MM time format Commented Nov 1, 2015 at 19:11
  • You wouldn't want a time 24:53 now, do you? So why <= 24 and not <24? Also, is a single digit hour permitted? Commented Nov 1, 2015 at 19:17
  • sorry the <= my mistake but the format must be hh:mm. Commented Nov 1, 2015 at 19:23

2 Answers 2

1

For validating, use a regular expression:

time.matches( "(?:[01][0-9]|2[0-3]):[0-5][0-9]" )

To convert, simply use

int hour = Integer.parseInt(time.substring(0, 2));
int minute = Integer.parseInt(time.substring(3));
Sign up to request clarification or add additional context in comments.

Comments

0

To follow your format, the user must enter a time with ':' at the 3rd character position charAt(2) and the rest of the characters (at indices 0, 1, 3 and 4) must be digits. A very quick to implement and good to learn solution is to use regex: http://www.regular-expressions.info/tutorial.html

However, I'll be showing you a simpler to understand solution using your code as a template:

  1. Verify that the length is 5
  2. Verify that the hours are correct
  3. Verify that the minutes are correct
  4. Ask the user to re-enter the value if anything is incorrect

Here's the code:

    int total; // Total Minutes
    String time; // Input from keyboard

    final Scanner T = new Scanner(System.in);

    System.out.print("Enter the time in HH:MM :");
    time = T.next();

    boolean correctFormat = false;
    int hour = 0, minute = 0;

    while (!correctFormat) {
        correctFormat = true;

        if (time.length() != 5)
            correctFormat = false;
        else {

            // Get the value before the ':'
            hour = Integer.parseInt(time.substring(0, 2));
            if (hour >= 24 || hour < 0)
                correctFormat = false;

            // Get the value after the ':'
            minute = Integer.parseInt(time.substring(3, 5));
            if (minute >= 60 || minute < 0)
                correctFormat = false;
        }
        if (!correctFormat) {
            System.out.print("Pleaase follow the format! Enter the time in HH:MM :");
            time = T.next();
        }

    }

    // Formula of the calculation
    total = hour * 60 + minute;

    // Display the final value
    System.out.println(time + " is " + total + " minutes.");

Further, if you want to make the code even more robust, you can check to see if the user actually enters numbers before and after the ':' by catching a 'NumberFormatException' that will be thrown from the Integer.parseInt() method if the parameters aren't an actual number. You can do this by editing the above code to as follows:

    int total; // Total Minutes
    String time; // Input from keyboard

    final Scanner T = new Scanner(System.in);

    System.out.print("Enter the time in HH:MM :");
    time = T.next();

    boolean correctFormat = false;
    int hour = 0, minute = 0;

    while (!correctFormat) {
        correctFormat = true;

        if (time.length() != 5)
            correctFormat = false;
        else {
            try {
                hour = Integer.parseInt(time.substring(0, 2));
                minute = Integer.parseInt(time.substring(3, 5));
            } catch (NumberFormatException e) {
                correctFormat = false;
            }
            if (correctFormat) {
                if (hour >= 24 || hour < 0)
                    correctFormat = false;

                if (minute >= 60 || minute < 0)
                    correctFormat = false;
            }
        }
        if (!correctFormat) {
            System.out.print("Pleaase follow the format! Enter the time in HH:MM :");
            time = T.next();
        }
    }

    // Formula of the calculation
    total = hour * 60 + minute;

    // Display the final value
    System.out.println(time + " is " + total + " minutes.");

1 Comment

This isn't really validating since your code runs into an exception due to invalid input.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.