This seems like a simple problem, but I can't seem to solve it. I need to print output (from a variable) to the console, until the user presses any key. The printing is done in a while loop as such:
int i;
while((i=input.read())!=-1){
System.out.print((char)i);
}
input.close();
The problem is that the input stream never reaches it's end, only waits for more characters to come in, thus blocking any more code from executing. It will be quite obvious to the user when to stop the output, so I am fine with giving that responsibility to the user. It doesn't matter how the user tells the application to stop, as long as it is quick, (eg pressing space, enter, or any other specific key is fine).
Thanks in advance,
vikarjramun
The first solution I came up with was threading the console input. The thread updates a variable accessable by the printing code so you can check it before each print statement.
import java.util.Scanner;
import java.util.concurrent.atomic.AtomicBoolean;
public class RunUntilKeyPressed {
static AtomicBoolean hasUserPressedKey = new AtomicBoolean(false);
public static void main(String[] args) {
Thread t = new Thread(() -> {
Scanner scan = new Scanner(System.in);
System.out.println(scan.next());
hasUserPressedKey.set(true);
scan.close();
});
t.start();
while(!hasUserPressedKey.get()) {
System.out.println(hasUserPressedKey);
}
}
}
This code will run until the user presses Any Key (except space) and Enter as that is when the scan.next() method gets input.
Related
Following is the program for reversing a word using a stack. There is no problem in implementation of stack. It is executing finely. When ctrl-c is pressed, it prints the content of try-catch block twice or even thrice. What causes this?
import java.util.NoSuchElementException;
import java.util.Scanner;
class WordReversal {
public static void main(String args[]) {
StackIFace sfWord; // interface reference
Stack s = new Stack();
sfWord = s;
Scanner sc = new Scanner(System.in);
System.out.println("Control-C to stop.");
for(;;) {
String senWord = "", revWord = "";
try {
System.out.println("Enter a word: ");
senWord = sc.next();
for(int i=0;i<senWord.length();i++)
sfWord.push(senWord.charAt(i));
for(int i=0;i<senWord.length();i++)
revWord+=sfWord.pop();
System.out.println("Original Word::"+senWord+"\nReversed Word::"+revWord);
} catch(NoSuchElementException NSEe) {
System.out.println("Control-C command accepted.\nQuitting...");
//break;
}
}
}
}
One thing to notice here is if I put break (just remove two forward slashes in above code), the problem is solved but exactly what is causing this? Is Scanner causing this or is it a language problem?
NoSuchElementException is not actually "intercepting ctrl/C". So what's likely happening is you're terminating input, the next() call throws an exception because there is no input, you print a message, and loop round to do it again.
Input is still terminated, so the next() call throws an exception....
What surprises me is thus not that it loops, but that it stops looping after 2 or 3 times. Maybe some other exception gets thrown that you don't catch; hard to say without adding a handler to check.
But the root issue is: if you want that particular exception to quit the program, you need to write code that quits the program. You in fact did that when you wrote the 'break' out of the loop.
You're using an infinite for loop and not telling it when to stop.
Adding break statement is terminating the for loop. Hence, it is working.
So I've tried to implement a solution for waiting on user to press enter to continue found here: Java Console Prompt for ENTER input before moving on
However, when I try to use either of the solutions proposed I get the following errors:
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.base/java.util.Scanner.nextLine(Scanner.java:1651)
at printLine.promptEnterKey(printLine.java:153)
at printLine.main(printLine.java:144)
and
java.io.IOException: Stream closed
at java.base/java.io.BufferedInputStream.getBufIfOpen(BufferedInputStream.java:176)
at java.base/java.io.BufferedInputStream.read(BufferedInputStream.java:342)
at java.base/java.io.FilterInputStream.read(FilterInputStream.java:107)
at printLine.promptEnterKey(printLine.java:155)
at printLine.main(printLine.java:146)
I made a test program before to make sure this would work as expected and it worked fine for me:
// import scanner for user-input
import java.util.Scanner;
public class test{
public static void promptEnterKey(){
Scanner scanner = new Scanner(System.in);
scanner.nextLine();
}
public static void main(String[] args){
promptEnterKey();
}
}
Here's my code, I am working on a program that will read the current line, print it and then wait for the user to hit enter before proceeding:
// import System.out
import static java.lang.System.out;
// import scanner for user-input
import java.util.Scanner;
// import File class
import java.io.File;
// import FileNotFoundException
import java.io.FileNotFoundException;
// delete if ioexception not used
import java.io.IOException;
/* this is the main public class */
public class printLine {
/* this method is used to execute the application */
public static void main(String[] args){
// create scanner for user input
Scanner userInput = new Scanner(System.in);
// user input for file
out.println("This program will print the text file line by line, waiting for the user to hit the enter key");
out.println("Please specify the file to print line by line: ");
String textFile = userInput.nextLine();
userInput.close();
// try to open file
try{
// load the file
File text = new File(textFile);
// for reading the file
Scanner textReader = new Scanner(text);
// while there is another token...
while (textReader.hasNextLine()){
String curLine = textReader.nextLine();
out.println(curLine);
promptEnterKey();
}// end while
// close reader
textReader.close();
}// end try
// catch FileNotFoundException error
catch (FileNotFoundException e){
out.println("File not found.");
e.printStackTrace();
}// end catch
}// end main
/* This method is for waiting for the user to press the Enter key.
this was taken from https://stackoverflow.com/questions/26184409/java-console-prompt-for-enter-input-before-moving-on */
public static void promptEnterKey(){
Scanner scanner = new Scanner(System.in);
scanner.nextLine();
}
}// end class
Here's the sample text excerpt that I am trying to use it on:
There was nothing so VERY remarkable in that; nor did Alice
think it so VERY much out of the way to hear the Rabbit say to
itself, `Oh dear! Oh dear! I shall be late!' (when she thought
it over afterwards, it occurred to her that she ought to have
wondered at this, but at the time it all seemed quite natural);
but when the Rabbit actually TOOK A WATCH OUT OF ITS WAISTCOAT-
POCKET, and looked at it, and then hurried on, Alice started to
her feet, for it flashed across her mind that she had never
before seen a rabbit with either a waistcoat-pocket, or a watch to
take out of it, and burning with curiosity, she ran across the
field after it, and fortunately was just in time to see it pop
down a large rabbit-hole under the hedge.
In another moment down went Alice after it, never once
considering how in the world she was to get out again.
The rabbit-hole went straight on like a tunnel for some way,
and then dipped suddenly down, so suddenly that Alice had not a
moment to think about stopping herself before she found herself
falling down a very deep well.
Either the well was very deep, or she fell very slowly, for she
had plenty of time as she went down to look about her and to
wonder what was going to happen next. First, she tried to look
down and make out what she was coming to, but it was too dark to
see anything; then she looked at the sides of the well, and
noticed that they were filled with cupboards and book-shelves;
here and there she saw maps and pictures hung upon pegs. She
took down a jar from one of the shelves as she passed; it was
labelled `ORANGE MARMALADE', but to her great disappointment it
was empty: she did not like to drop the jar for fear of killing
somebody, so managed to put it into one of the cupboards as she
fell past it.
Any insight as to what I'm doing wrong is appreciated.
Thanks!
JT
you are closing the userInput (System.in) immediately after reading the file name userInput.close();
move that line to the end of your code
Scanner.close will also close it's underlying readable if it implements Closable interface, in your case that is System.in input stream:
https://www.tutorialspoint.com/java/util/scanner_close.htm
This is a diary program which allows you to write something in your diary (obviously). After typing enter and pressing enter, the page closes and its gonna be safed in a list. My problem is that it only runs once when I have Pages(); in the main method, so I tried this loop. It doesnt work for me and i dont know why. Need some help
import java.util.ArrayList;
import java.util.Scanner;
public class NotizbuchKlasse{
public static void Pages() {
System.out.println("day 1 : Write something in your diary.");
System.out.println("Write enter if you are done writing.");
ArrayList<String> List = new ArrayList<String>();
String ListInList;
Scanner write = new Scanner(System.in);
do {
ListInList = write.next();
List.add(ListInList);
} while (! ListInList.equals("enter"));
List.remove(List.size()-1);
write.close();
System.out.println("This is now your page. Your page is gonna be created after writing something new.");
System.out.println(List);
}
public static void main(String[]Args){
boolean run = true;
do{
Pages();
} while(run);
}
}
Error:
This is now your page. Your page is gonna be created after writing something
new.
Exception in thread "main" [hello]
day 1 : Write something in your diary.
Write enter if you are done writing.
java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at NotizbuchKlasse.Pages(NotizbuchKlasse.java:12)
at NotizbuchKlasse.main(NotizbuchKlasse.java:24)
You need to check whether there is something to read before you read it. You're not currently, and that's why you're getting a NoSuchElementException.
You do this via Scanner's has* methods.
For example:
ArrayList<String> List = new ArrayList<String>();
Scanner write = new Scanner(System.in);
while (write.hasNextLine()) {
String ListInList = write.nextLine();
if (ListInList.equals("enter")) break;
List.add(ListInList);
}
// No need to remove the last item from the list.
But also, I notice that you have a loop in your main method, where you call Pages() in that loop. If you close write, you also close System.in; once a stream is closed, you can't re-open it. So if you try to read things from System.in the next time you call Pages(), the stream is already closed, so there's nothing to read.
Simply don't call write.close(). You shouldn't close streams that you didn't open in general; and you didn't open System.in (the JVM did when it started up), so don't close it.
You want to be using a while loop like this:
while (write.hasNextLine()) {
ListInList = write.nextLine();
if (doneWriting(ListInList)) { // Check for use of enter.
break; // Exit the while loop when enter is found.
}
List.add(ListInList); // No enter found. Add input to diary entry.
}
where doneWriting() is a method (that you write!) which checks to see if the user has typed enter.
Here is the documentation for the next() method of Scanner. If you read it, you will see that it throws the exception you are getting when it runs out of tokens.
If you want a little bit more a casual explanation here is a question that was asked previously about next() versus nextLine().
I want to add an integer to a list based on user input. The user has to type all the integers he/she wishes then press enter. if they finish inputting integer, they are supposed to press the "enter" button without typing anything.
I have made my code, but there are several mistakes
the exception keeps popping up because every time say for example I enter integer 10, then I finish. I press "enter" with nothing. this raises the exception. how do I tackle this problem?
and another thing, how do I make the program so that if the user puts invalid input, instead of crashing or breaking. It asks the user again to prompt the correct input.
this is what I have done
package basic.functions;
import java.util.*;
import java.text.DecimalFormat;
public class Percent {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
reader.useDelimiter(System.getProperty("line.separator"));
List<Integer> list = new ArrayList<>();
System.out.println("Enter Integer: ");
while (true) {
try {
int n = reader.nextInt();
list.add(Integer.valueOf(n));
} catch (InputMismatchException exception) {
System.out.println("Not an integer, please try again");
break;
}
}
reader.close();
}
}
output
Enter Integer:
10
Not an integer, please try again
[10]
I'd suggest you utilise Scanner#hasNextInt to identify whether an integer has been entered or not. As for when the "user presses enter without typing anything", we can simply use the String#isEmpty method.
while (true) {
if(reader.hasNextInt()) list.add(reader.nextInt());
else if(reader.hasNext() && reader.next().isEmpty()) break;
else System.out.println("please enter an integer value");
}
note - in this case, you don't need to catch InputMismatchException because it won't be thrown.
while (true) is generally a bad sign, if you ever have that in your code you are almost certainly wrong.
What you probably want is something like this:
String input;
do {
input = reader.next();
// Parse the input to an integer using Integer.valueOf()
// Add it to the list if it succeeds
// You will need your try/catch etc here
while (!input.isEmpty());
Here the loop is checking the exit condition and running until it meets it. Your processing is still done inside the loop as normal but the program flow is a lot cleaner.
I have a program that needs to read lines of input. It needs to be many lines at once. For example:
As I enter my time machine or
maybe not,
I wonder whether free will exists?
I wonder whether free will exists
maybe not
as I enter my time machine or.
That all gets entered at one time by the user. I was trying to use .hasNextLine() method from Scanner class, but it is not returning false.... it waits for input again. Ive been looking around for a solution and it appears that .hasNextLine() waits for input, but i do not know what alternative to use. Any suggestions? The actual code looks like:
while(input.hasNextLine());
{
line += input.nextLine();
}
Thanks for your help
Perhaps you should use some sort of "stop" sequence meaning when the user enters a particular character sequence, it will break out the loop. It might look something like:
public static void main(String args[]){
final String stopSequence = "/stop";
final Scanner reader = new Scanner(System.in);
String input = reader.nextLine();
while(!input.equalsIgnoreCase(stopSequence)){
//process input
input = reader.nextLine();
}
}