Is there a way to check if the user is typing in the console window in Java? My program prints information it receives about client connections using System.out.print() and I want it to stop printing information temporarily while the user is typing. User input is read on a separate thread using the Scanner class. I need to be able to see if the user has typed anything and still have whatever the user has typed (if anything) available to the scanner. If it's possible I would like to avoid using external libraries and just stick with the java libraries.
No, there are no ways to do that directly using the console (that I'm aware of) - the content of the console will only get sent to the application once enter is pressed and will then be available to the Scanner.
One way to solve it is to make your own console, that you can read and write from. Then you'll be able to do anything you want really (including check if anything is highlighted and so on). If you don't know how to code a GUI, you should look into that. Oracle has a tutorial on GUI with Swing.
The thread that listens to the user console , Generates an interrupt when the user starts typing, while the other thread that prints the information need to reset the interrupt, all you have to do is implementation of the scenario.
I think you're looking for Thread.sleep(milliseconds);. Using this until the user is done, you should get what you want.
Next time, please elaborate on your questions.
Related
I'm learning to program in Java and I'm wondering if there is a simple way to set a return point in a method in case a user decides they want to go back. For example, within a method, I ask the user to choose between one of three options:
1) Register
2) Search
3) Other
Let's say that the user chooses to register. This choice then presents them with two new options:
1) Register as User
2) Register as Admin
Let's say that they choose to register as an admin. They are then prompted to enter their information, but maybe halfway through they realize "Wait! I want to register as a user, not an admin!". Typing in "back" is also an option, and if they do so, I want to be able to take them back to the menu where they choose between user and admin, not all the way back to the beginning. Is there a way to do this? I know I could use loops, but my program is a bit more complicated than my example, so I would rather not if I can get around it. I'm looking for a way similar to the way you can name loops in assembly language, so that you can just say "go to this point" and it goes back without the user having to re-enter all the information that they did before the sub menu that they want to get to. (I realize that in my example they don't enter any info until that sub menu. In my actual program they do.)
Any ideas? If worse comes to worse, I'm not opposed to using a loop. Just figured I would ask!
Thanks so much!
I think using loops is as good a method as any. Two suggestions:
you might want to read up on the break-with-label statement; and/or
you might want to place each menu into a separate function to make the structure clear.
Let's say you have methods for each of these options. At any point if user wants to go to some option, just return from the existing method with some value which indicates the option user wants to jump to. From the main code, just go to the appropriate method depending on the option selected.
Also, each method may take input of the parent (id), so going back won't be a problem.
This way you can go back to the parent or to some other option from any point in your program.
I think you are looking at it the wrong way. You don't need to go back to a point in your code to do an undo or rewind, you need to create the logic to be able to change a decision and make it work with whatever data has been entered so far.
So for example, you would use a data structure to hold all the data that a user enters, and if he chooses to change his registration type, you'll be able to fill in his info from that data structure. He can also decide that the address he gave you is wrong and change it, you'll still want to keep all the data you got so far, regardless if he's changing from user to admin.
The point is, you are looking at this in a sequential way, as if code can only run forward and backward in a straight line, while Java and even assembly are not at all sequential, you can go in every direction, skip to some logical point in time, and jump back to where you were.
I'm making a server, and it is on a Text Based Raspberry Pi. basically, everything is running from the command line, so when the server runs, there is no graphics, and it prints everything out using System.out.println();. so my question is, instead of having a button that runs a shutdown() method, how can i make it so at any point in time, i am able to push, say, 'e', and the program will run the shutdown() method? i've done some searching, and am not sure quite how to phrase the question. i was thinking adding a keylistener, but im not sure if that can be added to nothing graphic? anyway, any help would be appreciated!!! thanks in advance
To register keyboard events you first need to have the focus on your program, and for that you need a gui. I suggest:
1.- Create a JLabel(and a scrollbar).
2.- Instead of using System.out.print("text");, use myJLabel.append("text" + "/n");.
3.- Add a keyboard listener. Register key events so that the x key closes your server, the s key stops it, ...
As you have described your app, you can't write input to the server, you can only read output from it. I recomend step 4.
4.-Add a JTextFiel to send input to the server.
5.-To make it user-friendly , you could use a JEditorPane instead of a JLabel, and add HTML to your output.
Alright, so based on the comments, (which i up voted btw), i made a thread that constantly used scanner to see if i typed "exit". Thanks for all the help!
If you don't want to have a visible gui, you could create an "always-focused invisible grafical interface" with the listener.
I created a sample Java application. I want to clear the window options, i.e.:
Register
Login
Clear
If the user presses 3 I need to programmatically clear all options. Something like Console.clear?
Is there any way that I can do this with Java?
You will need to output a bunch of blank lines. Even in Windows/*nix, clear/cls doesn't truly clear the screen, it just prints enough blank lines that you cannot see the previous text.
You can try System.out.print("CLS");
Or use loops to clear the screen like
public static void clearScreen() {
System.out.print("\033[H\033[2J");
System.out.flush();
}
and then call this method clearScreen(); if you want to clear.
Sorry my english is bad. :) I just want to help you.
You mean you created a console application and want to clear the console (not necessarily the Eclipse console)?
If so then I guess you're looking for:
Runtime.getRuntime().exec("cls");
But be aware this will be system dependent.
If you are working with the console, then these might prove useful.
Using "backspace" character.
Using process builder.
I'm using JNLP to print some images inside an applet. I pass a Printable object to a javax.jnlp.PrintService instance and call PrintService.print(). Is there any way to know when this operation completes?
Looking at line 116 of the PrintService source code, it seems to start a thread for printing and not attach anything to keep track of it.
There seems to be mechanisms for tracking print operations in other parts of Java, but I have not had success using other printing mechanisms in the context of a browser applet. (the user is constantly nagged about security)
Is it possible to print something in a signed java applet, not have the user be nagged about security, and know when the print operation finishes?
I managed to solve this problem by switching from using javax.jnlp.PrintService to using javax.print.PrintService. Using the later allows you to attach a PrintJobAdapter to monitor the status of the print job. See this Stack Overflow question.
All of this is perfectly acceptable in a JNLP deployed applet, provided the applet is signed.
This question sort of extends my other question on robots and captcha. I did what everyone recommend (thanks everyone!), however is it at all possible to detect a robot on the server first? For Example (Once again, I will use Stackoverflow as a reference): Sometimes when I ask a question, Stackoverflow comes back asking me to verify if I am human.
However, sometimes it does not.
How does Stackoverflow do that, because that is what I want to do: Check data and if it looks like a robot, request human verification.
Also this needs to be done on Java (preferably), Perl or PHP.
Thanks
On StackOverflow, it's done by performing the same task too many times too quickly or performing multiple tasks too quickly.
If you want to emulate this, you can keep track of the number and time(s) of recent requests and check to see that everything is within your limits. If it isn't, redirect to a CAPTCHA.
Unfortunately, I don't have enough Java EE experience to provide any code, but hopefully my approach will give you some idea(s).
The simple method would be to log activity (clicks, comments, ect.) and then check the frequency and similarity between these. You can usually detect robots by looking for similar tasks performed repeatedly.
If you are really serious about robot detection, log every keystroke and mouse movements. Regular users have a percentage of error and uncertainty associated with typing and navigating the site. A 100% typo free user that navigates the site easily and quickly (moving the mouse on a straight line from point a to point b) without ever going for the back button is very likely to be a bot.