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 9132363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:17:59+00:00 2026-06-17T08:17:59+00:00

I would like to continuously prompt the user to enter a date in a

  • 0

I would like to continuously prompt the user to enter a date in a given format, until he/she gets it right.

This is what I’ve done:

def readDate(prompt: String): Date = {
    var date: Option[Date] = None
    Iterator.continually {
      val startDateString = readLine(prompt)
      val startDate = catching(classOf[ParseException]).opt(asDate(startDateString))
      date = startDate
      startDate
    }.takeWhile(_ == None).foreach {
      date =>
        println("Incorrect format. Try again.")
    }
    date.get
  }

where asDate just uses SimpleDateFormat.parse on the entered String.

Now, this seems to work, but I’m pretty sure it’s not the right way.

I don’t really understand how to handle these chained iterators (since both Iterator.continually and takeWhile return an instance of AbstractIterator).

I have basically two questions:

1) Is there a way to “return” startDate from Iterator.continually? I’ve tried and failed map-ping it. I want this in order to get rid of the var date and date = startDate.

2) If I didn’t want anything to happen between the reads, what would I do with the last foreach? I’ve seen that nothing works if I just remove it (I think because of next() not being invoked), but is it OK to leave it there like this:

takeWhile(_ == None).foreach { date => {}} ?

Is there a better way than the “empty” foreach?

Thanks!

  • 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-06-17T08:18:00+00:00Added an answer on June 17, 2026 at 8:18 am

    You should use find instead of takeWhile–this will keep dropping entries until a good one comes through. Then you have an Option, so you just need

    def readDate(prompt: String): Date = {
      Iterator.continually {
        catching(classOf[ParseException]).opt(asDate( readLine(prompt) ))
      }.find(_.isDefined).get
    }
    

    if you don’t want to print anything. If you do want to print something, you can put it into the loop.

    def readDate(prompt: String): Date = {
      Iterator.continually {
        catching(classOf[ParseException]).opt(asDate( readLine(prompt) )) match {
          case None =>
            println("Incorrect format.  Try again.")
            None
          case x => x
        }
      }.find(_.isDefined).get
    }
    

    I’m not sure that this is dramatically clearer, but it’s definitely shorter and does those things you wanted.

    I’d probably use a tail-recursive function instead:

    import annotation.tailrec
    @tailrec def readDate(prompt: String, again: Boolean = false): Date = {
      if (again) println("Incorrect format.  Try again.")
      catching(classOf[ParseException]).opt(asDate( readLine(prompt) )) match {
        case Some(date) => date
        case None => readDate(prompt, true)
      }
    }
    

    The logic seems a bit clearer to me here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to perform the following algorithm - this must be done in
Would like to parse IPv4 address from exit-addresses . Format of the file: ExitNode
What I would like to do is write a small program that continuously live
I would like to create a simple content slider that would slide content continuously
I found this interesting interface(starts at 33 seconds http://vimeo.com/22946428 ), and would like to
I would like to continuously retrieve my emails via POP3 in my RoR-Projekt where
I would like to have an event triggered in my app which runs continuously
I would like a function to check a Queue for new additions continuously on
I would like to send data from a server to a client, continuously (streaming),
I would like to solve the following problem: Given a link on a Web

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.