1

I am getting a String from front-end which I don't know exactly how it looks like - depending on the user. So it can be

23 or 234 or 23:34 or 00:32

In this case, the problem is 23 and 234 because I need a format like hhh:mm. How can I do this in a performant way to always format the string in this format [h]hh:mm

4
  • the amount of hours can be two digets or three digest. Commented Feb 3, 2016 at 5:46
  • 1
    post the code you are tried Commented Feb 3, 2016 at 5:49
  • There can be 3 digit hours? Does 234 in the question mean 0234 (02:34 AM) Commented Feb 3, 2016 at 5:53
  • no, 234 hours means 234 hours and 0 minutes - so 234:00 [hhh:mm] Commented Feb 3, 2016 at 6:06

1 Answer 1

3

that's all I got from the question

    Scanner scn = new Scanner(System.in);
    String in = scn.nextLine();
    String time="";
    if(in.length()<=3)//23 or 234
    {
        time += in+":00";
    }
    else//23:34 or 00:32
    {
        time += in;
    }
    System.out.println(time);
Sign up to request clarification or add additional context in comments.

Comments

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.