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

  • Home
  • SEARCH
  • 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 8582975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:25:40+00:00 2026-06-11T21:25:40+00:00

I have written this code that prompts the user to enter their month of

  • 0

I have written this code that prompts the user to enter their month of birth and year of birth. There is some error checking: their month must be an integer and also in range from 1-12. The same for year. As it stands, I have two while loops and I can’t think of a way to combine them to still get the error message when something is wrong. I can think of it if it was just going to say “Bad input: reenter” but it needs to say what is wrong with the input. It starts with month and that month has to be good before it moves on to asking the year. If the year is wrong, it will then ask to reenter year — not go all the way back to month.

If anyone has any ideas, I’d appreciate it. The class used to check if it is an integer is one I’m required to use.

while(!monthDone) {
           System.out.print("Enter Month of Birth: ");
           monthInput=scan.next();

           if(!(Type.isInteger(monthInput))) {
                  System.out.println("You need to enter an integer for month");
                  monthDone=false;
           } else {
                         MOB = Integer.valueOf(monthInput);

                         if (MOB<1 || MOB>12) {
                               System.out.println("Your month is out of range");
                               monthDone=false;
                         } else {
                               monthDone=true;
                         }

                         while(!yearDone) {
                                     System.out.print("Enter Year of Birth: ");
                                     yearInput=scan.next();
                                      if(!(Type.isInteger(yearInput))) {
                                             System.out.println("You need to enter an integer for year");
                                      }
                                      else {
                                             YOB = Integer.valueOf(yearInput);
                                             if (YOB<1912 || YOB>2012)  {
                                                    System.out.println("Your year is out of range");
                                                    yearDone=false;
                                                    }
                                                    else
                                                    yearDone=true;
                                             }
                                      }
                               }
                         }
         } // else closed
 } // Outer while close

I am just posting the while loop. When both monthDone && yearDone == true, I then go on to print how old they are, etc.

  • 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-11T21:25:41+00:00Added an answer on June 11, 2026 at 9:25 pm

    This is how I might lay it out without so much nesting.

    Scanner scanner = new Scanner(System.in);
    int month, year;
    for (; ; scanner.nextLine()) {
        System.out.println("What is your month and year of birth as [1-12] [1900-2012]");
        if (!scanner.hasNextInt()) {
            System.out.println("Your month is not a number");
            continue;
        }
        month = scanner.nextInt();
        if (month < 1 || month > 12) {
            System.out.println("Your month is out of range");
            continue;
        }
        if (!scanner.hasNextInt()) {
            System.out.println("Your year is not a number");
            continue;
        }
        year = scanner.nextInt();
        if (year < 1900 || year > 2012) {
            System.out.println("Your year is out of range");
            continue;
        }
        break;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is the code that i have written this is showing error as shown
I have this code in my ASP.NET application written in C# that is trying
I have written this code what it does is if user types postcode or
I have written this code that loops through an array of lat/long coordinates and
I have written this piece of code that splits a string and stores it
I have this code that I have written in Ruby, but when trying to
I have written this code (this is a snippet) that doesn't seem to be
I have written this code in visual basic. On executing no error is printed
Hi I have written this code that with output you can get that .remove()
I have written this code for add cart to payment. while clicking buttons that

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.