Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4025270
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:49:33+00:00 2026-05-20T10:49:33+00:00

I’m trying to separate the hours, minutes, seconds, and milliseconds from an inputed time.

  • 0

I’m trying to separate the hours, minutes, seconds, and milliseconds from an inputed time. Right now I have

public MillisTime(String str)
throws IllegalArgumentException {
    // Initialize values so that missing fields default to 0.
    int hours = 0, minutes = 0, seconds = 0, millis = 0;

    // Use Scanner class to parse the time string.
    // Catch InputMismatchException and rethrow IllegalArgumentException.
    Scanner scn = new Scanner(str);

    scn.useDelimiter(":");
    try {
        if (scn.hasNext()) hours = scn.nextInt();
        if (scn.hasNext()) minutes = scn.nextInt();
        if (scn.hasNext()) seconds = scn.nextInt();

        scn.useDelimiter(".");
        if (scn.hasNext()) millis = scn.nextInt();
    }
    catch (InputMismatchException ex) {
        throw new IllegalArgumentException(
                                           "String Input Mismatch Exception: " + str);
    }
    scn.close();
    this.setAllFields(hours, minutes, seconds, millis);
}

The input it is receiving is "16:5:7.009" and results in a String Input Mismatch Exception. If I take out the lines that look for the period and millis, and input something like 3:45 it works. How do I make this work to find : and .?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T10:49:34+00:00Added an answer on May 20, 2026 at 10:49 am

    The exception occurs because with a delimiter of “:”, the subcomponents are {16, 5, 7.009}. 7.009 is not an integer so when you call scanner.nextInt() for the third time you get the exception.

    Keep in mind that the delimiter scanner uses can be a regEx pattern, so it’s possible for the delimiter to be . OR : at the same time:

    scanner.useDelimiter(Pattern.compile("[\\.:]");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is what i have right now Drawing an RSS feed into the php,
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a text area in my form which accepts all possible characters from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to loop through a bunch of documents I have to put
I have a bunch of posts stored in text files formatted in yaml/textile (from
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.