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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:55:16+00:00 2026-06-10T03:55:16+00:00

This code checks if the users input it is valid. if it is not

  • 0

This code checks if the users input it is valid. if it is not a number it will continue to loop untill it receives a number. after that it will check if that number is in bounds or lesser than than the bound. it will continue looping until it receives an inbound number. but my problem here is that when I print choice it only shows the previous number after the last number that was inserted. why is it like that?

public void askForDifficulty(){
    System.out.println("Difficulty For This Question:\n1)Easy\n2)Medium\n3)Hard\nChoice: ");
    int choice = 0;
    boolean notValid = true;
    boolean notInbound = true;
    do{
        while(!input.hasNextInt()){
            System.out.println("Numbers Only!");
            System.out.print("Try again: ");
            input.nextLine();
        }
            notValid = false;
            choice = input.nextInt();
    }while(notValid);

    do{
        while(input.nextInt() > diff.length){
            System.out.println("Out of bounds");
            input.nextLine();
        }
        choice = input.nextInt();
        notInbound = false;
    }while(notInbound);

    System.out.println(choice);
}
  • 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-10T03:55:18+00:00Added an answer on June 10, 2026 at 3:55 am

    This is because input.nextInt() inside the while condition consumes the integer, so the one after it reads the following one. EDIT You also need to combine the two loops, like this:

    int choice = 0;
    for (;;) {
        while(!input.hasNextInt()) {
            System.out.println("Numbers Only!");
            System.out.print("Try again: ");
            input.nextLine();
        }
        choice = input.nextInt();
        if (choice <= diff.length) break;
        System.out.println("Out of bounds");
    }
    System.out.println(choice);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This code checks for dead links. Since 3rd party Ajax requests are not allowed
So I have this code that checks 4 parameters (author, title, keyword and subject)
This portion of code checks if a number given (First element in a list)
I have this code snippet inside a function that checks if an object exists
I've got this code which checks if an url is a valid url. Right
I'm using this piece of code to read users input and check if it
I have this code in a html file that checks for connectivity, but the
This code works for me except it only checks for a duplicate once, and
I found this code on the internetz, it checks the current page url; function
I have this method which does certain checks and applies a color code on

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.