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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:57:46+00:00 2026-06-12T18:57:46+00:00

Hello I’m a beginning programming Student and I am practicing using loops to validate

  • 0

Hello I’m a beginning programming Student and I am practicing using loops to validate input. Unfortunately, the loop works but skips the inner loop entirely… I get now error message or prompt…

Here is my code: [I BORROWED IT FROM AN ANSWER ON THIS SITE ABOUT VALIDATING INPUT SO I COULD TEST IT.]

import java.util.Scanner;

public class ValidationTest
{
public static void main(String[] args)
{
    Scanner sc = new Scanner(System.in);
    int number;
    do {
        System.out.println("Please enter a positive number!");
        while (!sc.hasNextInt()) 
        {
            System.out.println("That's not a number!");
            sc.next(); // this is important!
        }  
        number = sc.nextInt();
       } while (number <= 0);
    System.out.println("Thank you! Got " + number);
}
}
  • 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-12T18:57:47+00:00Added an answer on June 12, 2026 at 6:57 pm

    The inner loop :

     while (!sc.hasNextInt()) 
        {
            System.out.println("That's not a number!");
            sc.next(); // this is important!
        }  
        number = sc.nextInt();
    

    only check if your input is not a number, if you input -123, the function !sc.hasNextInt() is false so it’ll skip the loop, if you want to check the number is negative, add this check after the assign number value:

     if(number <= 0 ){
         System.out.println("The number is negative!");
     }
    

    You don’t have to make another loop for checking the number is negative or not because of the first loop had it, and do...while loop will make sure you have to run the loop at least one time

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

Sidebar

Related Questions

Hello! I've been programming for a long time but just started developing for android,
Hello I want to delete all data in my postgresql tables, but not the
Hello iam trying to show a rect for a bmi graph. but the rect
Hello i started using Laravel framework for my Facebook apps lateley and i created
Hello everyone i am trying to format the input number range with php number_format
Hello , I am using arshaw fullcalendar v1.5.2 (only month view ) for property
hello creating a custom object may be a widely published topic, but my lack
Hello i have a TextField on my scene. It haves only digits, user input
Hello i been trying to get a tokenizer to work using the boost library
Hello everyone, I'm developing a photo sharing web site using the CodeIgniter PHP framework

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.