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

The Archive Base Latest Questions

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

Trying to get user input as an int between 0 and 100, and prompt

  • 0

Trying to get user input as an int between 0 and 100, and prompt the user to “try again” as long as their input does not match this criteria.

My code thus far is somewhat successful at accomplishing my goal:

import java.util.Scanner;

public class FinalTestGrade
{
    public static void main(String[] args)
    {
        Scanner studentInput = new Scanner (System.in );
        int testGrade;

        System.out.println("Please enter your test grade (0 to 100)");
        while (!studentInput.hasNextInt()) 
            {
            System.out.println("Your input does not match the criteria, please enter a number between 0 and 100");
            studentInput.next();
            }
        testGrade = studentInput.nextInt();

        while (testGrade > 100 || testGrade < 0) 
            {
            System.out.println("Your input does not match the criteria, please enter a number between 0 and 100");
            testGrade = studentInput.nextInt();
            }

As you can see, the program will check that the input is an int. Once the user successfully enters an int, the program checks to make sure that their input is between 0 and 100. The problem arises when the user enters a non int in response
to the second prompt (initiated by the second while loop). Below is an example:

run:
Please enter your test grade (0 to 100)
P
Your input does not match the criteria, please enter a number between 0 and 100
109
Your input does not match the criteria, please enter a number between 0 and 100
P
    Exception in thread "main" java.util.InputMismatchException
            at java.util.Scanner.throwFor(Scanner.java:840)
            at java.util.Scanner.next(Scanner.java:1461)
            at java.util.Scanner.nextInt(Scanner.java:2091)
            at java.util.Scanner.nextInt(Scanner.java:2050)
            at finaltestgrade.FinalTestGrade.main(FinalTestGrade.java:24)
Java Result: 1
BUILD SUCCESSFUL (total time: 9 seconds)

So long story short, I’m wondering if there’s a way to combine my while loops so that input expressed as an int between 0 and 100 is accepted & saved as a variable. All input which doesn’t meet this criteria should trigger a prompt which repeats until the input meets this criteria. Any suggestions?

  • 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-13T12:09:15+00:00Added an answer on June 13, 2026 at 12:09 pm
    int testGrade = -1 ;
    Scanner studentInput = new Scanner(System.in);
    while (testGrade > 100 || testGrade < 0) 
    {
       System.out.println("Your input does not match the criteria, please enter a number between 0 and 100");
    
       while(!studentInput.hasNextInt())
       {
           studentInput.next() ;
       }
       testGrade = studentInput.nextInt();
    }
    

    Have an infinite loop to check if there are invalid characters in the stream. If so, consume it, that is what hasNextInt() is for. If you enter something valid, that loop exits.

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

Sidebar

Related Questions

I'm trying to get edit text to behave in this way: User can input
I am trying to get this code to verify that the user input a
I'm trying to query my database based on user input and I get this
I'm trying to get input from user using array of structs and then print
I am trying to get a user to enter a number between 1 and
I am trying to get input from the user and need to know a
I'm trying to figure out how to use scanf to get user input. I
I'm trying to get user GUID from Active Directory. My code: DirectoryEntry entry =
hi I am trying to get user profile information like this once the authentication
I am trying to get the user defined global hot key for my application.

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.