Endless loop - scanner.hasNextInt() inside a while loop - java

The questions are in the comments of the code, sorry for that, I thought it's neater, as the flow is important, I guess...
import java.util.Scanner;
public class ReadingUserInput {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Please, enter 10 numbers, for example, from 1 to 100!");
int number = 0;
int total = 0;
int counter = 0;
while (counter < 10) {
System.out.println("Enter number #" + (counter + 1));
boolean hasNextInt = scanner.hasNextInt(); // here we open the prompt for user to enter the value/s*
// internally, we are ready to check if the input is going to be int
// user types the value/s and clicks enter
// let's presume, he/she typed '3'
// internally, user's input is like that (if Windows**) - '3\n'
// because when user presses Enter - \n is added to what he/she typed
if (hasNextInt) { // the app checks, ant it's int, that is, it's OK (true)
number = scanner.nextInt(); //here the application grabs user's input
//but, internally, it grabs only '3', because 'nextInt()' grabs only ints
// and doesn't "care" about the new feed/line - \n - character
// so, '\n' is left in Scanner's buffer!
counter++;
total += number;
} else {
System.out.println("Invalid Input! Try again!");
}
//scanner.nextLine(); // let's presume, this commented line, on the left of this line of comment, is absent in our code
// the flow of our code goes to boolean hasNextInt = scanner.hasNextInt();
// and again internally, we are ready to check if the input is going to be int
// and again the user is prompted (by a blinking cursor) to type his/her input
// and at this moment user types either a numeric again or a non-numeric character (a letter/letters)
// let's presume he/she is typing '4'
// and again, internally, user's input is actually like that (if Windows**) - '4\n'
// but scanner.hasNextInt() says 'OK', for the int is there! and it doesn't care about '\n'
//
// Now, let's presume that user (this time or next time) types 'a'
// Do we actually have 'a\n' ???
// and this time scanner.hasNextInt() says 'Alarm' - 'false'
// thus the input doesn't go to number = scanner.nextInt();
// So, does it mean that 'a\n' (or 'a') remains in Scanner's buffer???
// and it (scanner.hasNextInt()) kicks us to 'else'
// and we have an endless loop:
//Invalid Input! Try again!
//Enter number #...
//Invalid Input! Try again!
//Enter number #...
//Invalid Input! Try again!
//Enter number #...
//Invalid Input! Try again!
//Enter number #...
//Invalid Input! Try again!
//Enter number #...
//Invalid Input! Try again!
//Enter number #...
//Invalid Input! Try again!
//Enter number #...
//Why?
// Is there still 'a' (or 'a\n') and scanner.hasNextInt() throws the flow to 'else' endlessly,
// because "The scanner does not advance past any input"* ???
//
// or: there's only '\n', and again its not int, and we result in endless loop ???
// And finally, is that a different case? https://www.youtube.com/watch?v=_xqzmDyLWvs
// And PS: Is there anything wrong in my description in the comments?
// So what do we 'consume' by scanner.nextLine(); ???
}
scanner.close();
System.out.println("Thank you, your total is " + total);
}
}
// *This is from Oracle :(https://docs.oracle.com/javase/6/docs/api/java/util/Scanner.html#hasNextInt%28%29)
"hasNextInt
public boolean hasNextInt()
Returns true if the next token in this scanner's input can be interpreted as an int value in the default radix using the nextInt() method. The scanner does not advance past any input."
// **https://knowledge.ni.com/KnowledgeArticleDetails?id=kA00Z0000019KZDSA2

Create another scanner object instead and forget about what is left in the internal buffer.
public class ReadingUserInput {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Please, enter 10 numbers, for example, from 1 to 100!");
int number = 0;
int total = 0;
int counter = 0;
while (counter < 10) {
System.out.println("Enter number #" + (counter + 1));
boolean hasNextInt = scanner.hasNextInt();
if (hasNextInt) {
number = scanner.nextInt();
counter++;
total += number;
} else {
System.out.println("Invalid Input! Try again!");
scanner = new Scanner(System.in);
}
}
scanner.close();
System.out.println("Thank you, your total is " + total);
}
}

Related

Java Sum of numbers until string is entered

i've just started java programming and was wondering on how to approach or solve this problem i'm faced with.
I have to write a program that asks a user for a number and continually sums the numbers inputted and print the result.
This program stops when the user enters "END"
I just can't seem to think of a solution to this problem, any help or guidance throughout this problem would be much appreciated and would really help me understand problems like this. This is the best i could do
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
while (true) {
System.out.print("Enter a number: ");
int x = scan.nextInt();
System.out.print("Enter a number: ");
int y = scan.nextInt();
int sum = x + y;
System.out.println("Sum is now: " + sum);
}
}
}
The output is supposed to look like this:
Enter a number: 5
Sum is now: 5
Enter a number: 10
Sum is now: 15
Enter a number: END
One solution would be to not use the Scanner#nextInt() method at all but instead utilize the Scanner#nextLine() method and confirm the entry of the numerical entry with the String#matches() method along with a small Regular Expression (RegEx) of "\d+". This expression checks to see if the entire string contains nothing but numerical digits. If it does then the matches() method returns true otherwise it returns false.
Scanner scan = new Scanner(System.in);
int sum = 0;
String val = "";
while (val.equals("")) {
System.out.print("Enter a number (END to quit): ");
val = scan.nextLine();
// Was the word 'end' in any letter case supplied?
if (val.equalsIgnoreCase("end")) {
// Yes, so break out of loop.
break;
}
// Was a string representation of a
// integer numerical value supplied?
else if (val.matches("\\-?\\+?\\d+")) {
// Yes, convert the string to integer and sum it.
sum += Integer.parseInt(val);
System.out.println("Sum is now: " + sum); // Display Sum
}
// No, inform User of Invalid entry
else {
System.err.println("Invalid number supplied! Try again...");
}
val = ""; // Clear val to continue looping
}
// Broken out of loop with the entry of 'End"
System.out.println("Application ENDED");
EDIT: Based on Comment:
Since since an integer can be signed (ie: -20) or unsigned (ie: 20) and the fact that an Integer can be prefixed with a + (ie: +20) which is the same as unsigned 20, the code snippet above takes this into consideration.
Do it like this:
public static void main(String[] args) throws Exception {
int sum = 0;
Scanner scan = new Scanner(System.in);
while (scan.hasNext()) {
System.out.print("Enter a number: ");
if (scan.hasNextInt())
sum += scan.nextInt();
else
break;
System.out.println("Sum is now: " + sum);
}
System.out.print("END");
}
This will end if the input is not a number (int).
As pointed out in the comments, if you want the program to stop when the user specifically enters "END", change the else-statement to:
else if (scanner.next().equals("END"))
break;

How to use a Do-while loop that continuously prompts a user?

(I have a homework question that I've been stuck on that concerns "do-while loops" in Java. )
It is asking me to have a do-while loop that continues to prompt a user to enter a "number less than 100", up until the entered number is actually less than 100.
(It will run three tests:)
Ex: Test 1:
For the user input 123, 395, 25, the expected output is:
Enter a number (<100):
Enter a number (<100):
Enter a number (<100):
Your number < 100 is: 25
(Here's my code so far:)
public class NumberPrompt {
public static void main (String [] args) {
Scanner scnr = new Scanner(System.in);
int userInput = 0;
do {
System.out.println("Enter a number (<100):" );
System.out.println("Enter a number (<100):" );
System.out.println("Enter a number (<100):" );
userInput = userInput + 25;
} while (userInput > 100);
System.out.print("");
System.out.println("Your number < 100 is: " + userInput);
}
(My Output matches exactly with the Test 1 results above, but I realize I'm not setting up the loop right at all because when it does the second test of "-9", the output exactly the same as my first test:)
Enter a number (<100):
Enter a number (<100):
Enter a number (<100):
Your number < 100 is: 25
(This is my first week being introduced to loops, I searched around for some walk through examples but I haven't found many "Do" while loops that remind me of this one. If anyone has some good tips or guides to point me to I would greatly appreciate it.)
You don't need to write System.out.println("Enter a number (<100):" ); three times. It will be displayed automatically every time when user inputs value less than 100. And you are assigning value to userInput by your own instead of taking input from user. You should write the code given below.
do {
System.out.println("Enter a number (<100):" );
userInput = scnr.nextInt();
} while (userInput > 100);
System.out.println("Your number < 100 is " + userInput);
1.Reading value
As Juan's comment suggested, you are not reading user input value. Basically, this is done with nextInt() method. Morever, int is a primitive type and must be initialised. You choose the value of 0. So why not choosing 101 so that it starts with an incorrect value? Then you are sure that the while loop will be triggered:
public static void main(String... aArgs) {
Scanner sc = new Scanner(System.in);
// initialise at 101 to trigger the while loop
int userInput = 101;
// as we start at 101, it will enter the loop at least once
while (userInput > 100) {
System.out.println("Enter a number (<100):");
userInput = sc.nextInt();
}
System.out.println("Your number < 100 is: " + userInput);
}
2.Never trust the user
2.1 Exception catching
The above code may be sufficient for your assignement. However, for learning sake, I have a fundamental principal: never trust the user!. If the previous example, if an user inputs azerty, then your program will throw you an InputMismatchException. What's that? This error tells you that the scanner expected an int and you fed him with something else: it throws this up.
A simple analogy is: the scanner can only eat apple. You give him a pear: he takes a bit and throws that up: "I can only eat apple". To prevent your scanner from throwing up, you can ask him: "try a bit, and if this is not an apple, let's try something else"
In code, it gives you something like:
public static void main(String... aArgs) {
Scanner sc = new Scanner(System.in);
// initialise at 101 to trigger the while loop
int userInput = 101;
// as we start at 101, it will enter the loop at least once
while (userInput > 100) {
// tell the scanner to try something
try {
System.out.println("Enter a number (<100):");
userInput = sc.nextInt();
}
// if the input is not a number, tell him do this this:
catch (InputMismatchException e) {
System.out.println("This is not a number!");
}
}
System.out.println("Your number < 100 is: " + userInput);
}
If you are not familiar with try/catch clause, you can read this
2.1 Scanner feeding
The above code is not working. How? If you enter something which is not a number, like "aaaa", you will have an infinite of
Enter a number (<100):
This is not a number!
Why? Because the scanner did not throw your input out. Basically, he either should eat the pear (but he will throw up) or throw it to the dust bin but you never told him to throw it to the dust bin! The scanner needs to consume the input before trying the next input.
In a nutshell:
userInput starts at 101 so the while loop is entered
You enter aaaa.
The InputMismatchException is caught. The "This is not a number!" is printed out
userInput value did not change (still at 101), so the loop keeps going
At this stage, the scanner did not consume the previous input so the next input is still aaaa.
Go to 3. and start again
How to fix this? By telling the scanner to consume the input with next():
public static void main(String... aArgs) {
Scanner scnr = new Scanner(System.in);
// initialise at 101 to trigger the while loop
int userInput = 101;
// as we start at 101, it will enter the loop at least once
while (userInput > 100) {
// tell the scanner to try something
try {
System.out.println("Enter a number (<100):");
userInput = scnr.nextInt();
}
// if the input is not a number, tell him do this this:
catch (InputMismatchException e) {
// consume the incorrect input with scnr.next()
// so that user can enter another input
System.out.println(scnr.next() + " is not a number!");
}
}
System.out.println("Your number < 100 is: " + userInput);
}

Using java scanner to check two conditions while taking user input

I need to user to enter an int between 1 and 301.
I have this simple loop here to check for user input.
I just want a single number from the user, and if the user enters anything other than an int between 1 and 301, I want to display the print line and prompt the users to try again until they enter a valid input.
while (!sc.hasNextInt()) {
System.out.print("Invalid Input. Please enter a valid number between 1 and 301: ");
sc.next();
}
int numToCheck = sc.nextInt();
// do stuff with numToCheck
This checks that the input is an int, but I can't seem to find a way to give the int input a bound. I tried to assign the user input to a variable and then check the conditions input < 1 or input > 301, but I get InputMismatchException if user enters a letter. How should I store the user input? (I want to store it as an int to check the conditions, but can't do that since I don't know what the user will enter).
Perhaps there is a better design to accomplish all this. Those are welcomed too.
Thanks in advance.
You're not saving the value of the of the input. So your program is waiting on the user to enter a number each time it see "sc.nextInt()" Assign the input to a variable, and then check the condition.
EDIT: okay, I'll go the extra mile for you. See if this works.
***Accounted for the case where the user might enter a character instead of a number.
import java.util.*;
public class HelloWorld{
public static void main(String []args){
Scanner sc = new Scanner(System.in);
int input;
while (true){
if (sc.hasNextInt()){
input = sc.nextInt(); // Assign the next integer to a variable
if (input <= 301 && input >= 1){ // Check if integer meets condition
break; // Condition met, break out of loop
}
}else{
sc.next();
}
System.out.println("Invalid Input. Please enter a valid number between 1 and 301: ");
}
}
}
I ran this code, to see if it would show a better performance than yours.
Scanner sc = new Scanner(System.in);
boolean valid = true;
do {
if (!valid) {
System.out.print("Invalid Input. ");
}
System.out.print("Please enter a valid number between 1 and 301: ");
String input = sc.next();
try {
int value = Integer.parseInt(input);
valid = (value >= 1 && value <= 301);
} catch (NumberFormatException nfex) {
valid = false;
}
} while (!valid);
When the conversion to integer fails, the JVM hangs a little. I believe your problem has more to do with the try / catch mecanism that Scanner performs under the hood, than with design.
Assuming you want only 1 input from the user, try following simple code, which takes input from the user until user enters a valid input.
Scanner in = new Scanner(System.in);
int flag = 0,x=0;
while(flag == 0){
x = in.nextInt();
if(x<1 || x>301){
flag = 0;
System.out.println("Invalid Input.");
}
else{
flag = 1;
}
}
And if you want user to input more than 1 inputs (i.e 3 here), than set a counter that increases with every valid input of the user, as following:
Scanner in = new Scanner(System.in);
int flag = 0,x=0,count = 1;
while(flag == 0){
x = in.nextInt();
if(x<1 || x>301){
flag = 0;
System.out.println("Invalid Input.");
}
else{
//executes when input is valid
if(count == 3){
flag = 1;
}
count++;
}
}
Edit:
If you also want to check whether the input is Integer or not, than you have to add one extra condition in above code. And as you said you want only one input from user rather than 3, you have to change exit condition. Change code as following:
Scanner in = new Scanner(System.in);
int flag = 0,count = 1,x=0,flag1 = 0;
String y;
while(flag == 0){
y = in.next();
flag1 = 0;
try{
x = Integer.parseInt(y);
}
catch(NumberFormatException e){
flag1 = 1;
System.out.println("Invalid Input.");
}
if((x<1 || x>301)&&flag1 == 0){
flag = 0;
System.out.println("Invalid Input.");
}
else if(flag1 == 0){
//executes when input is valid
if(count == 1){ // put count == 3 if you want 3 inputs from user.
flag = 1;
}
count++;
}
}
Here we are taking the input as a String and than converting the String into the Integer by using Integer.parseInt(). If the String is not Integer, than it will throw the exception and we will continue the loop till the valid input is entered by the user.
Use DO WHILE for result
do{
System.out.print("value of x : " + x );
x++;
System.out.print("\n");
}while( x < 20 );
OK ?

How to continue loop inside of Try Catch for Guessing game

So my problem is that i don't know how to continue my program when i do the try and catch for input errors. I tried using the "continue;" code after my catch statement but that just loops my program uncontrollably. I need the program to start where it left off after the user does an incorrect input. Any help would be appreciated. Note that this was an assignment BUT I'm going above and beyond by handling junk in my code.
//Import library
import java.io.*;
import java.util.*;
//File name
public class GuessingGame
{
//Main throws Input and output error
public static void main (String [] args) throws IOException
{
//inputs for users
Scanner in = new Scanner (System.in);
Scanner i = new Scanner (System.in);
//variables for the loop, random number, character and counter
int guess = 0;
int rnd;
char decision;
boolean loop = false;
//random number generator
Random random = new Random();
rnd = random.nextInt(100) + 1;
//loops the guess and input
while (!loop){
try{
System.out.println(rnd);
//prompt the user
System.out.println(" Please guess a number between 1-100. Press 0 to exit.");
int num = in.nextInt();
//if statements
if (num==0)
{
//when user types '0' it ends the program
System.exit(0);
System.out.println("You gave up!.... Reseting program...");
}
else if (num>rnd)
{
//prints too big, adds to counter 'guess'
System.out.println("The number is too big!");
guess++;
}
else if (num<rnd)
{
//prints too small, adds to counter 'guess'
System.out.println("The number is too small!");
guess++;
}
else
{
//prints correct, adds to counter, dsiplays # of guesses and ends loop
System.out.println("You guessed the number right!!");
guess++;
System.out.print(" # of guesses: " + guess);
//Note**: i could've just put 'break;' but the compiler would'nt read the rest of the code below
loop = true;
//loops the case untill correct input is chosen either 'Y' or 'N'
while(true){
//prompt the user if they want to play again
System.out.println(" Would you like to play again? Y/N?");
decision = i.nextLine().charAt(0);
switch (decision) {
case 'Y':
case 'y':
//calls main, basically restarts the game
GuessingGame.main(args);
break;
case 'N':
case 'n':
System.out.println("Bye!");
//exits the program completely
System.exit(0);
break;
default:
//if incorrect input, this prints
System.out.println("Please enter a Yes or No <Y/N>");
}
}
}
}
//catches input errors
catch (Exception e){
System.out.println("Only numbers!!!");
//GuessingGame.main(args);
continue;
}
}
}
Try this move your catch up because you are only testing the input. Also add in.nextLine() in your catch to eat up the character that is left behind.
while (!loop){
int num;
try{
System.out.println(rnd);
//prompt the user
System.out.println(" Please guess a number between 1-100. Press 0 to exit.");
num = in.nextInt();
}
catch (Exception e){
System.out.println("Only numbers!!!");
//GuessingGame.main(args);
in.nextLine();
continue;
}
Scanner by default splits the standard input by spaces and keeps an index of how many substrings have been parsed. The specific method you call (.nextWhatever) will attempt to parse the next string in line to its expected type and will only increase the index if it's successful; if there's no stream left to parse, it will await new input.
The reason your loop is infinite is because it failed to parse the token to an integer and isn't increasing the index. There are two ways to skip the invalid input. nextLine() will skip the rest of the stream waiting. For example, if the input was "1 abc 2"
in.nextInt(); // equals 1
// in.nextInt() would fail
in.nextLine(); // equals "abc 2" and if put in your catch would clear the stream
However, if you want to keep trying subsequent tokens (in this case skip "abc" but try "2", which is valid), next() is more appropriate because it will just skip over one token.
try(){
// validate input
int num = in.nextInt();
}
catch(Exception e){
System.out.println("Ignoring your faulty input");
in.next();
}

Testing for blank input using Scanner class during input validation

I need to ask the user to enter a positive non-zero integer from the console that I will use as a product number in my program.
If I enter any non-integer value, the program correctly enters the while loop.
If I enter a 0 or a negative integer, the program correctly throws the exception (which I catch and handle elsewhere).
When I just press the enter key (or end of line character) it seems that the program just puts the console to another line. I want it to enter the while loop to display the error message. I think the cause is that hasNextInt() will wait until the token is a non end of line character input.
private static void validateProductNumber() throws InvalidProductNumberException {
Scanner keyboard = new Scanner(System.in);
int number;
while(!keyboard.hasNextInt()) {
System.out.println("Number must be an integer. Try again. ");
System.out.println("Enter a new number: ");
keyboard.next();
}
number = keyboard.nextInt();
if (number <= 0)
throw new InvalidProductNumberException();
newNumber = number;
}
Is there another way I can implement my input validation with Scanner class so that it works correctly for all situations?
You can change your loop as follows:
while(true) {
try {
System.out.println("Enter a new number: ");
//read the line and parse it
number = Integer.parseInt(keyboard.nextLine());
break; //break the loop if the input is a valid integer
} catch(Exception ex) {
//print the error message if the input is incorrect
System.out.println("Number must be an integer. Try again. ");
}
}
if (number <= 0)
//...

Categories

Resources