scanner doesn't close using scan.NextInt() and .hasNext() loop - java

I'm trying to read the Input below but the Scanner doesn't close in Java. From the output, I can see all the inputs are taken inside the string, but the console still shows the program is still running. The red square button is still active. Can you please help?
4
0 3
2 5
4 2
4 0
Scanner scan = new Scanner(System.in);
String input = "";
scan.useDelimiter("\\s*");
while (scan.hasNext()) {
System.out.print(scan.nextInt());
}

The scanner doesn't know when you are going to stop entering data at the console (unlike reading from a file which signals an EOF). So it blocks, waiting for more input. So you have several options.
You can either type Ctrl-D (or what ever your OS requires to signal an EOF).
You can use special input to stop checking (Like a "Done" string) (which means you will need to parse ints if you want integer input too.)
Or you can initially prompt for an integer to indicate how much data to type in via a loop.
You can also do the following:
while (true) {
if (!scan.hasNextInt()) {
// requires a non-empty and non-numeric value.
break;
}
System.out.println(scan.nextInt());
}
But do not close the Scanner after reading from the console. Otherwise you will not be able to reopen it and take input within the same program.

I guess you are new to Java and software development. First, you should check the JavaDoc of the hasNext() method:
https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/Scanner.html#hasNext()
Citation: "This method may block while waiting for input to scan."
So you have a continuous loop. If you want to exit your loop, you have to do that explicitly.

Related

Don't understand how Java Scanner object is working in this example (hasNextInt, next...?)

In my program I need to take an input from a user, but it must be an integer. If the user doesn't input an integer, I need the program to reprompt them to enter an integer, and keep doing so until they enter one. I found an example while loop code snippet online which works perfectly, but I am having problems understanding why and how it works, and I'd really like to understand better:
Scanner reader = new Scanner(System.in);
int guess=0;
System.out.println("Guess the number");
while (!reader.hasNextInt()) { //I get that this is a "not have" boolean
System.out.println("That's not a number. Please enter a valid number");
reader.next();
}
guess= reader.nextInt();
System.out.println(guess);
My questions are:
Does the !hasNext condition in the while loop actually trigger the scanner object to ask the user for input and then check that input, or does it only check anything that has already been inputted?
Why is it necessary to have the reader.next(); line after the while loop's main statement and what is this doing exactly? I know it's necessary as the program doesn't work when I take it out. But is it prompting for input? If yes what happens to this input?
When guess= reader.nextInt(); runs, why doesn't it re-prompt the user for input at this point?
Sorry that these are probably really basic questions, I'm new to coding and Java and just can't get my head around what's happening internally in this particular example, though have no problem doing other basic stuff with the Scanner.
It's not particularly intuitive, for sure, so don't feel bad for not getting it immediately.
It seems to me that the problem you're having with your understanding is that that methods such as nextInt may or may not prompt for user input, depending if anything is already in the Scanner.
Here's the sequence of events:
All your code executes until you hit !reader.hasNextInt(). There's no input so it "blocks" (waits) until there is some input from the user.
If the user enters 'A', that's not an integer so we enter the body of the while loop. We then print the error message.
Now, hasNextInt doesn't "consume" (process) the user input when it's checking whether or not it's an integer, so we still have that invalid user input of 'A' sitting in our scanner. We call reader.next() to effectively discard that value.
Now we're back to !reader.hasNextInt(). The scanner is empty once again, so we prompt for user input. If they enter another non-integer, that process will simply keep repeating.
Say this time we do have a valid user input - they've entered the number 2. This passes the check so our while loop ends and we continue along.
We've now got some input in our scanner, but we've not consumed it. We're sure it's an integer because of the while loop condition. We can now consume the input with reader.nextInt() and assign it to our variable.
hasNextInt() only checks whether the next input is an integer. It won't consume any input at all. The ! is just negate theu outcome of this function.
As hasNextInt() won't consume, then we need to use next() to consumeo the user input to let hasNextInt() to check with user's next input value. As you won't need it at all, then no need to assign it to any variables.
Scanner won't display the prompt at all. The prompt is printed by System.out.println().
For the line nextInt(), it is used to consume the next user input. Since hasNextInt() must be true when it execute this line, there must be one integer input waiting for consumption, so this method can return immediately with that user input.
Try it :)
public static void main(String[] args) {
int option;
if(args!=null&&args.length>0){
option = Integer.parseInt(args[0]);
}
else{
System.out.println("Enter:\n 1 to run x \n 2 to run y \n");
Scanner keyboard = new Scanner(System.in);
option = keyboard.nextInt();
}
switch (option) {
case 1:
xx
break;
case 2:
yy
break;
default:
break;
}
}

Java System.in hasNext method works only first time it is called

This problem has me stumped. I've written a method which is called from inside a loop. The first time it works perfectly, after that it just hangs.
This is the method:
public static String promptUser(){
String path = "";
Scanner reader = new Scanner(System.in);
while (!path.contains(ROOT_FOLDER)){
System.out.println(String.format("Please paste in the directory path from WinSCP, including %s: ", ROOT_FOLDER));
while (true) {
if (reader.hasNext()){
path = reader.nextLine();
break;
}
}
}
reader.close();
return path;
}
And this is the loop
while (true) {
try {
listFiles(promptUser());
break;
}
catch (Exception e) {
e.printStackTrace();
System.err.println(e.getMessage());
}
}
The first time I run it, it will prompt the user as many times as it takes to get the info we need (a directory path). It then sends a request for that to an FTP server and if that path does not exist I want it to continue prompting the user. But my debugger is telling me on the second go round it just hangs on:
if (reader.hasNext()){
No amount of hitting the enter key gets it to continue. On the first invocation of promptUser I could enter something without the root folder and it would just keep asking until it got the root folder. So why doesn't it do that on the second invocation?
What is going on?
The first time you call promptUser it does the following:
Create a Scanner that wraps System.in.
Call hashNext which blocks until there is data to be read.
Read a single line.
Close the Scanner.
Return the line.
The problem is step 4. When you close the Scanner, you also close System.in.
Next time you call promptUser
Create another Scanner that wraps System.in. System.in is closed at this point.
Call hashNext() which immediately returns false ... because the stream wrapped by the Scanner is closed.
Repeat 2. ad nauseam. You have an infinite loop.
Solution: don't close System.in or a Scanner that wraps it. And don't just create a second Scanner that wraps System.in. Instead, reuse the original Scanner.
But is it necessary to close the scanner at some point?
You should never need to close a Scanner that wraps (the original) System.in stream. The reasons that we close streams are:
to cause buffered data to be written (for an output stream), and
to avoid file descriptor leaks.
The problem with leaking file descriptors is that a process (i.e. your JVM) can only have a certain number of files open at a time. If you leak file descriptors, you may find that attempts to open files start to fail with unexpected IOExceptions.
In the case of System.in there is no data to be written. Since you can't open System.in again (because you don't know what it is connected to) at most one file descriptor can be "leaked", so this is not a problem.
Eclipse warns that I should.
The Eclipse heuristics about closing scanners, etcetera are rather simplistic. In this case, the warning is probably a false alarm, though I would need to see the code to be sure.
You should not use break inside the loop when you fetch the next element. This causes your while(true) loop to exit. This means that you get the first item and finish.
You should change it to something like this:
while(reader.hasNext()) {
path = reader.nextLine();
// do something with the path here...
}
Read the documentation of hasNext()
Returns true if this scanner has another token in its input. This method may block while waiting for input to scan. The scanner does not advance past any input.
Returns:
true if and only if this scanner has another token
So, hasNext() is waiting for you to enter some value. It'll wait till it finds a single string entered by user.

Java "While" Loop -- Counting Matches Algo - Assigning value must occur inside the while loop

I apologize in advance; I couldn't find the answer to this simple question in Search.
I'm a newbie to coding. Currently on Udacity working my way through the Intro to Java Programming course.
There's something that I'm not understanding as it relates certain Algorithms.
Take the "counting matches" algo for example.
The assignment of double input = in.nextDouble(); needs to occur inside the while-loop.
If I place it just above the while-loop, it breaks the program. Why?
It seems to me that Java shouldn't care when the value is stored in the variable.
import java.util.Scanner;
public class CountingMatches
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int counter = 0;
System.out.print("Enter a value, Q to quit: ");
// double input = in.nextDouble(); // remember to place the assignment of this variable inside the while loop.
// I tend to want to place this outside the while loop because I still don't
// understand why it necessarily must occur inside the while loop.
while (in.hasNextDouble())
{
double input = in.nextDouble(); // this assignment is properly located here as opposed to just above the while-loop
if (input < 0)
{
counter++;
}
System.out.print("Enter a value, Q to quit: ");
}
System.out.println("The water line fell on " + counter + " years.");
}
}
Because in.nextDouble() can only be used if Scanner has already confirmed the next token can be parsed as a double. In addition to waiting for user input, this is what hasNextDouble() guarantees for you. If you take it out of the loop, not only are you skipping that hasNextDouble() guarantee (and not giving the user a chance to input anything), you are also only running nextDouble() one time, so you wouldn't have the newest value anyway.
Returns true if the next token in this scanner's input can be interpreted as a double value using the nextDouble() method. The scanner does not advance past any input.
-- https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html#hasNextDouble--
since you can´t do any further input when you move it outside the loop the condition for the loop in.hasNextDouble() will allways be true. Afterwards you will be stuck in an infinite loop, which makes the programm like like it´s brocken, but actually it´s just repetitively looping and printing until you stop the java process by yourself.
You are right that java shouldn't (and doesn't) care when the value is being stored in a variable, but there is more to it.
in.hasNextDouble() doesn't return until the line inside the wrapped input stream has ended.
It does not alter the stream in any way, but it guarantees there is a character sequence waiting in the stream and if true can be parsed as a double. The buffer is not altered until nextDouble() is called.
This is what breaks your code. When you remove the line from the buffer without it being parseable to a double an exception is thrown, because no characters are waiting in System.in

Continue reading console input after Ctrl+Z in Java

For a university assignment, from the console I need to be able to read in multiple lines until the user enters Ctrl+Z. That I have no issue with, my problem lies with me being unable to read anything in from System.in after that because it always throws a NoSuchElementException.
The following is the code that I am using for reading in the multiple lines, it was provided by the instructor and so I don't want to change it.
System.out.println("To terminate input, type the correct end-of-file indicator ");
System.out.println("when you are prompted to enter input.");
System.out.println("On UNIX/Linux/Mac OS X type <ctrl> d");
System.out.println("On Windows type <ctrl> z");
Scanner input = new Scanner(System.in);
while (input.hasNext()) {
//Processes input
addFileIntoDirectory(input.nextLine());
}
I understand that this is caused by the Ctrl+Z which equates to an EOF marker, but I don't know how to move past it. No matter how many reads after it I do, regardless of if I have typed more to the console, I just instantly get back another NoSuchElementException.
I have tried having a separate scanner for the menu, closing the scanner above and opening a new one for the menu but neither works.
Is there a way to flush/purge System.in or to reset it?
Please let me know if you would like more details. I have kept the rest of the program vague as it is homework.
EDIT 1: Assignment says "The system provides a textual selection menu as follows, and
runs in loop." Which means the program is not to terminate on Ctrl+Z.
add files from user inputs
display the whole directory
display the size of directory
exit
Please give a selection [0-4]:
The short answer is:
if (!input.hasNext())
input = new Scanner(System.in);
The long answer is:
Below is code, with comments made for removing later, to demonstrate the problem as I understand it and solution.
Note this demo is intended to only work with integer and ^Z inputs.
Copy out the code and save into file HN.java to compile.
Run the program and it will prompt One. Enter integers to your heart's content and exit the loop by entering ^Z.
Two will be displayed followed by the NoSuchElementFound exception in the question. This is because the ^Z remains in the Scanner object input and thereby fails on the nextInt() method.
The first inclination might be to use hasNext() to account for this. So go ahead and uncomment /*Comment1 and recompile and run again.
Now you avoid the exception, but the program blazes through to the end, skipping over the nextInt() originally intended.
So now, uncomment /*Comment2, recompile, and run again. Now the program will wait at the Two prompt as intended. If you enter an integer here, you will proceed to the Three prompt for another entry.
However, if you enter ^Z at Two, again, the program skips the next input. To correct this, uncomment /*Comment3, recompile, and run, and you will see the program works for all combos of integers and ^Z at the various inputs.
Now, you might be wondering why I didn't just reuse the !input.hasNext() solution in /*Comment3. Here's a demo why:
Put back the /*Comment3 and uncomment the /*Comment4, compile and run again. The program works fine and dandy with a ^Z input at the Two prompt, but if you enter an integer there, you'll see the system waiting for input, but there is no prompt Three!
This is because the integer you entered was used in the preceding nextInt() so when the program gets to the hasNext(), it stops and waits for input.
The lesson here is you use the !input.hasNext() when you KNOW you have a ^Z in queue such as when it was used to escape the while loop in this program and the original poster's question. Otherwise the else structure is better suited.
hasNext() is confusing to work with. You have to keep in mind that if nothing is in queue the program will stop and wait for input at that point. This may mess up your prompting if you're not careful.
import java.util.Scanner;
public class HN
{
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
int temp = 0;
System.out.println("One");
while (input.hasNext())
{
temp = input.nextInt();
}
System.out.println("Two");
/*Comment2
if(!input.hasNext()) input = new Scanner(System.in);
Comment2*/
/*Comment1
if (input.hasNext())
Comment1*/
{
temp = input.nextInt();
System.out.printf("%d\n", temp);
}
/*Comment3
else input = new Scanner(System.in);
Comment3*/
/*Comment4
if(!input.hasNext()) input = new Scanner(System.in);
Comment4*/
System.out.println("Three");
if (input.hasNext())
{
temp = input.nextInt();
System.out.printf("%d\n", temp);
}
System.out.println("Four");
}
}

Why doesn't Scanner seem to look for a new int every time nextInt is called

Why does it continue in the while loop repeating "Please enter a valid number" and it keeps repeating without stopping to let the user input something.
while (true) { //This will continually run until something is returned, AkA the number 1 or 2
Scanner s = new Scanner(System.in);
try {
input = s.nextInt();
} catch (Exception e) {
System.out.println("Please enter a valid number!");
}
s.close();
}
When I use the debugger nothing seems to be the cause of this problem and no errors are thrown. What is causing this problem and how can it be fixed?
Closing a scanner also closes the underlying stream if it implements the Closable interface.
That's System.in in this case and, once closed, you won't be able to create a scanner using it again.
See http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#close() for the gritty detail.
But, even if you fix that problem, an exception from nextInt will not advance the stream pointer so, the next time you call it, it will find exactly the same data in the input stream again.
You need to clear out the erroneous data before trying again. Since you're accepting user input, one solution is to call nextLine and throw that away;
string junk = s.nextLine();
Remove
s.close();
And Define your Scanner object outside the while loop as it not an Issue. Btw, I've tried your code, it worked fine until I entered a input which is not an int. When I entered a string, the exception sysout statement keeps printing infinitely
So you've to add
string junk = s.nextLine();
in your Exception block like paxdiablo said so that it wont repeat continuously.

Categories

Resources