Creating a Java Swing pop up window to request for details - java

i am beginning java swing programming and i was wondering how to do the following:
i want, as the program starts, that a pop up window will appear and request the user for something simple, his full name, and then assign it to a variable, String for instance.
i want that the user won't be able to use the program until he puts his full name.
i will do the checking to verify that what he wrote is his name(doesn't consist numbers or special letters, has a length not smaller than 3 etc..) - but my main goal is just creating that pop up window.
thank you in advance for your assistance and for helping me at my java swing learning curve.

String input = JOptionPane.ShowInputDialog("Enter your full name"); to read and assign it to a variable called input.
Full name might consists of several names and spaces.
Then use String methods like substring(), trim() and indexOf() to make sure you at least get first and lastname.
Visit https://www.w3schools.com/java/java_ref_string.asp for more String methods.
Hope this helps.

Related

How can I read keyboard inputs word by word

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.

Taking input from a text field in one class and scanning it in a different class

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.

accessing androids auto correct facilities?

Is there a way of using Android's auto correct / predictive text capabilities with a bespoke input method? I'd like to be able to access a list of the nearest words to the word entered, similar to what happens when we send a text. For example if I entered the string "hapy" I would get a list containing "happy", harpy", "hazy" ...
Looks like a yes.
And the place to start: http://developer.android.com/guide/topics/text/spell-checker-framework.html
You wouldn't want to. First off, predictive text was only turned into a service with 4.0. Before then it was just part of the keyboard, and most keyboards still implement their own I suspect. Secondly, it would be optimized for typing mistakes, not voice mistakes. Typing g instead of f is common (they're next to each other), doing it by voice is not. It wouldn't work well.
But the built in voice to text behavior does return alternatives- it returns an array list of possible texts. That is your auto-correct.

How to replace console input value with * in java?

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.

Getting an input with a java applet while executing a method

I have implemented a console calculator application. It stores variables as well. Now I want to use the same parser in a java applet. I want to use the same Parser class but this time I want to get the inputs for variables via a dialog box. It should stop the execution of the program and asks for a user input in a popup box and when I enter the value, I want to use that value in the next line of the application.
instead of getting the value from bufferedreader, i want to do sthg like this
if someappletclasss.dialogresult = ok,
value = someappletclass.myvalue;
I'm new to java programming and any help would be greatly appreciated.
Use JOptionPane#showInputDialog().
String inputValue = JOptionPane.showInputDialog("Please input a value");
See also:
The Java Tutorials - How to make dialogs?

Categories