How do i validate integer as user input?

Developers00 :

I am required to write a program that will validate the user input as integer. I have a code that will actaully work. But i dont really understand how it works.. Is like i know how to use, but have no idea how it works behind the program. Can you please explain to me how it actually work?

Also, some other alternatives may be using try catch here but i am not required too.. so can anyone please explain to me? i am still new to Java.

Really appreciate that !

while(!read.hasNextInt())  // i understood this pard at which the conditions will return true/false
    {
        System.out.println("Enter integer only: ");
        read.next();  // without this line of code, i will get an infinite loop, BUT WHY?
    }

    int num = 0;  // declaration of variable
    num = read.nextInt();  // and this actually store the last digit user input in read.hasNextInt()
                           // why would'nt it prompt the user to enter again? because usually it does
    System.out.print(num); // and finally output the num value
Jason :

Your while loop checks if the Scanner doesn't have a parsable Integer by blocking. If that condition is true, you are simply calling next() which clears the Scanners cache. If you didn't clear the cache it would always have an integer and would continue to block indefinitely. You need to call a method like next() or nextInt() to consume the value.

The call to nextInt() that you have doesn't request input again because you haven't consumed anything in your while loop, you just checked if the input was a parsable Integer.

This is the breakdown of your code (pseudo);

while scanner doesnt have a parseable integer {
    consume that non parseable value
}
consume the parseable integer

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How Do I Safely Scan for Integer Input?

How do i check the input is an integer in java

How do I validate two input fields on HTML form?

How do I check if an input is an integer or not in python?

How do I validate the password of a user created with the User Manager?

How do i validate user input that starts with 2 numbers?

How do I validate an input and change the target value if needed in Svelte?

How do I get user input to use my validate method for a login console application?

How do I validate a user's input and make sure it is the correct type and within a given range?

How can I validate input so that it must be an integer

How do I check to see if the input is an integer?

How to validate user input in python

Jquery how do I validate input field

How can I validate user-input in python?

How do I get a UITextField input as an Integer

How to validate user input in shiny

How do I keep asking a user for a valid integer input if there is more than one constraint to the input

How do I check if user input is an integer in Ruby?

ReactJS - How do I validate input fields

How do i validate a user input in python?

How can I detect if the user input is not an integer?

How do I validate input in Django REST framework before creation?

How do I validate a user input with int.TryParse to ensure the user entered an integer but also ensure the number was between 1 and 4

How do i scan each character in a user input and compare it to the second integer they input?

How can I validate a Perl regex in user input?

How do I validate user input in a loop?

How to validate user input in Java?

How do I use a function to validate user input within another function?

How do I remove an integer from an input?