Java Wait for Enter press (do not execute multiple keypresses) - java

I have a procedural Java Program that has multiple segments that I would like to execute manually (using "Press Enter to Proceed").
Right now I've been trying with System.in.read() or Scanner but if I would make multiple enter presses, it automatically executes the next segment.
how might I regulate this behavior?

I would suggest using BufferedReader instead of System.in.read() . It has a built-in flush mechanism too.

You have to remove everything from the InputStream before you wait for the next enter.
System.in.available() returns the number of bytes currently in the stream.
System.in.read(new byte[System.in.available()]);
This clears the Stream, so now the user has to enter something again.
Note: System.in.skip() doesn't work for some reason.

Related

Reading from keyboard and ignoring printed text

I am writing a java program (In Intellij) that accepts a command from the user and reacts to the command. The reaction is asynchronous (using a separate thread).
Scanner in = new Scanner(System.in);
String command = null;
do {
System.out.println("Enter command or q to exit: ");
command = in.nextLine();
System.out.println("Received "+command);
obj.react(command);
}while (!command.equals("q"));
The reacting object may take a while to react and prints a message after it finishes.
The problem is that if I start typing a command, and before I finish, the object prints something, the typed command is lost.
For example Here is a problematic scenario (The text in italics is the user input):
Enter command or q to exit:
go
Received go
goAgainobj finished reacting!
Received
In this case, when I Hit enter after the printed message, the received command is empty.
Is there any way to keep the typed characters even after something was printed?
If you use an actual console, printed output will not affect written input. If you type 'go' and the system prints 'Again', then the in-buffer still knows 'go'. This is unintuitive and bad to read, but it's practical to interrupt running scripts, or other programs.
This may already work on your IDE or your system, depending on OS ans IDE.
If you want something more 'pretty' then you need to fully control input and output, much like the 'top' command in linux (if you happen to know that).
You can handle this way of input better with the Console class. See: https://www.codejava.net/java-se/file-io/3-ways-for-reading-input-from-the-user-in-the-console #3
The most intuitive idea to solve your problem seems to read, and then remove all input at the time you want to print something, and reprint it, so you'd get:
> go
received go
obj finished reacting!
> go
...
You'd basically always print an input line yourself, after first reading and removing the already printed input. This is why you need the Console class, because there, input and output are synchronized, so if you print something, you know that no input will happen in the meantime.

Exit Java program when user sends EOF (either Ctrl-D or Ctrl-Z)

I want to read radiuses in a loop and exit the program when user sends an EOF (Ctrl+D / Ctrl+Z).
I'm not familiar with Linux and this combination.
Can anyone please explain what an EOF and these commands do? And how should I implement them?
From you program's perspective an EOF is an event when you call a read system call, usually performed by a library, which returns less bytes than requested. There is nothing special about it. It's the same when reading lines from STDIN or a file. No more lines, you exit your main loop.
Linux VTE (virtual terminal) devices arrange this for your program when you hit Ctrl+d.
How to do this in Java, I ain't no clue. I can't even read Java. I never find the needle of meaning in the haystack of boilerplate. :)

I want to know the difference between cvWaitKey(0) and cvWaitKey(10)

cvShowImage("SMART", cropped);
cvWaitKey(10);
cvShowImage("SMART", cropped);
cvWaitKey(0);
What is the difference between these two functions and when i use this in infinite for loop cvWaitKey(10) works but cvWaitKey(0) or cvWaitKey(30) do not work. What is the reason?
The difference can be found in the documentation of OpenCV of the waitKey function.
Basically, the function waitKey waits for a key to be pressed, and the argument is the amount of time it will wait. So, when you use 10. It will wait 10 milliseconds and then continue with the program.
The documentation says:
0 is the special value that means “forever”
So, when you use 0. The program will wait for key to be pressed forever... Just pressing any key will continue the program... (also closing the window will do it)
I recommend to use 10 when you are doing a stream of pictures (maybe from a camera). And use 0, when you are expecting human interaction for the program to continue. And a bigger value if you want to see it for enough time, but continue the program without any interaction.

Modify Control C Command Signal to Allow Input

Upon pressing Control C on the command prompt, is there a possibility of interrupting this signal, and prompting the user for input, to confirm they want to exit the program? I am aware that there are signal interrupts, but not on how to modify the signal to allow input.
String user;
Scanner input = new Scanner(System.in);
try {
[...] // Some code
}
catch(NoSuchElementException e) {
System.out.println("You have chosen to exit the program using Control C.\n");
System.out.print("Are you sure you want to exit? Type Yes or No.");
user = input.nextLine();
if(user.equals("yes")) {
System.exit(0);
}
else {
[...] // Return to the main menu
}
Currently, this code catches the Control C signal, outputs the String: "Are you sure you want to exit? [..]" but then refuses to accept input from the user. It waits a second, then the program would exit instead of getting the input from the user to confirm their decision.
However, I want the code to request input from the user upon Control C activation, and if it is yes, exit the program, else return to the main menu, which it seems to not work as intended.
The short answer is: You should really not do that. There is no portable way to prevent an interrupt signal from exiting a Java application.
The longer answer is that might be possible using internal classes such as sun.misc.Signal and sun.misc.SignalHandler. Since these classes are not portable across JVM implementations and may change in later versions you should avoid using them.
What you should do is to provide another standard way of exiting the application such as typing quit and require confirmation when receiving that input.
If you really want to avoid users exiting with ctrl+c you could try running java with -Xrs which should disable the handling of the console signal (NB: I have not tested this and am not really sure how it works)

About Java ProcessBuilder's stream

first of all.. apologize what i can't upload code..
(thirdparty program is sdelete)
I'm work with third Party program on java ProcessBuilder.
like..
ProcessBuilder("cmd","/C","thirdParty.exe")
or
ProcessBuilder("thirdParty.exe");
and then.. I make two thread that using scanner that get ProcessBuilder's stream.
(one is input Stream print , other one is error Stream print)
When I execute this program.
It seems like fine at opening part. thirdParty program's opening Messages show up in console and .. according to priority.. Process percentage must show up, but doesn't..
Percentage not show up, but it's not hangs or frozzen..
thirdparty work's fine. Just Scanner can't get InputStream data.
(if not with java process, namely if i just execute program.. percentage show up properly)
and.. finally when thirdParty process finished.. all of percentage data show up at once!
is anyone know about this phenomenon?
advice please..

Categories

Resources