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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:17:53+00:00 2026-06-15T05:17:53+00:00

I currently have a simple function (posted below) which asks the user a question

  • 0

I currently have a simple function (posted below) which asks the user a question and expects to have an integer answer.

Is there a way to make java restrict the characters that can be entered into the console i.e. only allow numbers to be entered.

I know there are simple ways to do this in other programming languages but How should I go about doing this in java and implementing it into my function?

    static int questionAskInt(String question)
{
    Scanner scan = new Scanner (System.in);
    System.out.print (question+"\n");
    System.out.print ("Answer: ");
    return scan.nextInt();
}
  • 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-15T05:17:55+00:00Added an answer on June 15, 2026 at 5:17 am

    Using Scanner.hasNextInt, and while loop, you can restrict the user to give input, until it passes an integer value.

    while (!scan.hasNextInt()) {
        System.out.println("Please enter an integer answer");
        scan.next();
    } 
    return scan.nextInt();
    

    or, you can also give a certain number of chances (so that it does not go into infinite loop, by using a count variable: –

    int count = 3;
    
    while (count > 0 && !scan.hasNextInt()) {
        System.out.println("Please enter an integer answer");
        System.out.println("You have " + (count - 1) + "more chances left.");
        count--;
        scan.next();
    }
    
    if (count > 0) {
        return scan.nextInt();
    }
    
    return -1;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Very simple + silly question: Does clojure provide multi maps? I currently have something
I have a very simple interface which needs to communicate between processes. It's currently
I've got a function which loads various models, and currently have this kind of
I currently have a function which will take an input and round it to
I have a simple function that shows and hides elements if a user clicks
I currently have inline functions calling another inline function (a simple 4 lines big
I have a simple function that do this: 1) User view some image and
I currently have a simple form that when you click the save button will
Currently I have a simple knockoutJS object, with a few observables. But this object
Currently I have a simple maven project that is building a jar file 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.