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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:38:14+00:00 2026-06-13T11:38:14+00:00

Basic problem here.. I will start off by asking that you please not respond

  • 0

Basic problem here.. I will start off by asking that you please not respond with any code, as that likely will only confuse me further (programming noob). I am looking for a clear explanation on how to solve this issue that I’m having.

I have a scanner that reads input from the user. The user is prompted to enter an int value between 1 to 150 (whole numbers only). I obtain the value as follows:

    Scanner scan = new Scanner(System.in);
    int input = scan.nextInt();

And continue on with my program, and everything works fine.

Unfortunately, the code isn’t exactly bulletproof, since any input that is not an integer can break it (letters, symbols, etc).

How can I make the code more robust, where it would verify that only an int was entered?

These are the results I’m hoping for:

Lets say the input was:

23 -> valid
fx -> display an error message, ask the user for input again (a while loop would do..)
7w -> error, again
3.7 -> error
$$ -> error
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-13T11:38:15+00:00Added an answer on June 13, 2026 at 11:38 am

    Scanner.hasNextInt() returns true if the next token is a number, returns false otherwise.

    In this example, I call hasNextInt(). If it returns true, I go past the while and set the input; if it returns false, then I discard the input (scanner.next();) and repeat.

    Scanner scan = new Scanner(System.in);
    while(!scan.hasNextInt()) {
        scan.next();
    }
    int input = scan.nextInt();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The basic problem I have here is that I have one xml file that
I have a basic ajax application, which will not work, instead the php code
Problem The basic problem is I want to debug some 3rd party code to
I have such a basic problem in Delphi,I can't solve it. My Code: Note:DataR
I am struggling with a very basic regex problem in my .htaccess file that
I have a basic problem but Google is not yielding many results for me
I have a problem here that still cannot solve, the thing is I have
PHP/MySQL newbie here. I've created a basic search engine that queries a MySQL table
Here's an interesting problem. I have an ETL script written in c# that I
My basic problem is to generate 2d renders of 3d objects, such as one

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.