Here's my problem, I'm writing a program that predict the most probable next word you will type.
So for instance if I just type "you" the program could propose you the word "are".
My problem has nothing to do with this exactly, but it's more on the implementation.
Right now, the only thing I can do is read words until I type enter, and then my program will give me a prediction for the last word of the sentence.
Basically what I would like to do is to stop reading everytime I type a whitespace, then my program respond by the word, and then I could type again another word.
I'm trying to look at the Scanner class, but I don't find anything that could help me here.
Thanks.
This is not possible. You would have to switch the console into raw mode, for which there is no multiplatform solution. You're much better of just making a window with a TextArea and using that instead the console window. That can be achieved in very few lines of code.
Related
I get that this isn't possible to do with normal java, although if there are any libraries out this it would be very useful.
Essentially, I'm designing a console app and running into an issue that when output happens while something is typed in the input line, that input text will move up and appear before the line that just got output. Is it possible to fix this in some form so that the text you are inputting that stays at the bottom?
EX:
I'm typing something as input into my commandline app, and then the program prints something WHILE I'm typing - this causes what was originally on the input line to be scrolled up with whatever the output text was. When you are trying to type something in this can obviously be detrimental. I know it's possible to prevent this.. (Other programs have done it... EX: Minecraft Server)
(If I need to be more descriptive I can.)
You could use the help of threads. One that listens to user input, the other process the actual output. This problem is similar to basic race condition problems when multiple threads attempt to read and write to a shared resource.
Your shared resource is that console. You need to keep the Input/Output operations synchronized. Have a look at race condition.
I am creating a very simple program, asking the user to guess words. Guessing words works just with Scanner and System.out.println(), so its very simple and no user interface is needed.
The guessing of words is done with eclipse. Now I need to add a function, which will allow the user to exit the "program" anytime by clicking CTRL+z and when they do that I'll need also to print out possible words they could've have guessed.
But I do not know how to add the CTRL+z exit function. Can anyone suggest anything on how to ? The word guessing is a loop.
You're answer is simple:
You can't do that!
Because you're using a command-line window. In command-line there is no listeners like KeyListener or MouseListener.....
If you want to do so, leave the command-line and go learn Swing in java.
See this question: How to get input without pressing enter every time?
I am trying to make a GUI for a program I have completed and do not want to modify. My problem is the program uses a scanner to get user input and I don't know how to get input from the GUI to the scanner. I know how to use getText() from a text field in the GUI but that doesn't update the scanner so the main program just waits for that input and doesn't continue.
"I am trying to make a GUI for a program I have completed and do not want to modify."
When you give restrictions such as this, it is usually a good idea to explain more, such as the reasons for the restriction; this understanding often helps us give better answers.
"My problem is the program uses a scanner to get user input and I don't know how to get input from the GUI to the scanner. I know how to use getText() from a text field in the GUI but that doesn't update the scanner so the main program just waits for that input and doesn't continue."
You are trying to do what is next to impossible. The solution: correct your console class so that the user interface portion is separated out, a la MVC, so that the model can be used for either a console program or a GUI program.
So the simple answer is: don't try to do this. Create a well behaved set of classes with decent separation of concerns so that you can re-use classes well.
I have a file I'm reading through android. It is essentially just a list of words. I have an edit text where the user enters a word, and then I need to check if that word is in the list of words. I can get the file, I then add all the words to a String, and I can check if the characters of the word entered are located in the list of words, but I can't check if it's the word alone or not. For example, if the user enters 'bro' and bro is not in the word list, but 'brother' is, it returns true. The words are each on their own line, so I tried testing 'bro' as '\nbro\n', but that doesn't work. Any ideas? If you need code posted let me know, but I don't think it helps much
I found what I was looking for. It was an unanswered question linked to at the bottom of the page. I needed to use a buffered reader to read the file, which allows for reading one line at a time. Thank you for your help. Using Buffered Reader
How can I print * in the place of input character which is entered from keyboard?
Example:
If I type in the console: mouli, then it should replace m with * and then o with * and so on.
There's no way of solving this using the standard API. If this is indeed an explicit requirement, you'll have to use some system specific library that interacts with the underlying terminal.
If the intention is to let the user enter a password however, I suggest you use Console.readPassword.
The console is not a "part" of Java. It's just one of many means of inputting information to your program. I think your question is more like:
"I'm reading a string in from the Keyboard, and I want to replace every character with an asterisk".
But I'm not sure if that's actually what you want.
Alternatively, if you're trying to make a "password" entry field in the console, where typed characters appear as asterisks, you may want to look into the Console class, seen here. However, I would lean towards the assumption that your purpose would be better suited by an actual GUI. The readPassword method only stops the letters from appearing on the screen, but doesn't replace them.