How to replace console input value with * in java? - 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.

Related

Creating a Java Swing pop up window to request for details

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.

Android password fields - input hidden at all times

As most of you know, at the moment, the password field works in a way that it hides the input, but not right away. As you input a character, it shows that character, then a moment later turns it into a *, I'm wondering if there's a way to incorporate a functionality that makes the characters stars at all times.
Any ways of doing this?
Don't set the default edit text property as password. Instead, you can use addTextChangedListener() which will get called as soon as the user enters a character. Maintain an activity level string say "mPass". In the TextWatcher(), onTextChanged method, append character to your mPass and replace the input character by *.
But you will have to be careful regarding this because application will pass control to the TextWatcher() even after you have replaced the character by *. If not handled properly it will get called recursively causing the application to crash.
Credit to Pallavi

Java CLI App - keep input from scrolling up

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.

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.

Is it possible to print text that can be edited by the user (for console programs)?

Say I allow the user to edit something, like the phone number in an Address Book (actually, that's exactly what I'm doing). Is there something that I can add to println that will allow me to insert a variable to display as fully editable text? The assignment that I'm doing this for doesn't actually call for this, but I think it would be cool to have. I'm looking on Google but can't find anything, then again I don't really know what I'm looking for and whether or not I have the correct terms in mind ...
No, not using only what Java provides in the framework. Editing some text would require to
act on key press, which is not possible as in Java the input is buffered (i.e., wait for Enter to be pressed)
to move around in the text you output, which is also not possible
This could be done using some native code (ncurse on linux, ...), using JNI or JNA, but not that easily.
Note that there are some projects that aim to add those functionalities, so if you can use something outside of the core libraries, you could give them a tries... for instance http://code.google.com/p/java-console-api/
There are various options for this, in order of simplicity and portability to features and complexity:
Simply prompt for the information, reading a complete (return-terminated) line of response, and allow the normal terminal input facilities to be used for basic editing.
Use something like the gnu readline library to allow more advanced editing. You still won't have widgets (text input boxes at specific places on screen) as such though. There's a java implementation here: http://java-readline.sourceforge.net/
Use something like ncurses to specifically position the cursor, print text labels, handle keypresses, and implement your own text input box. Not fun.
Use a textual user interface library (TUI), like this one: http://www.bmsi.com/tuipeer/
If you opened a window that looks like the console window, and could react to keypress events, then you could do what you are asking, but, otherwise, if you are just running a program, the program will have ceased executing and returned control to your console, so it can't do anything else.
But, if you use a scriptable version of java you could write your own shell, and then you could do what you are asking, as the shell would not cease executing.
But, that will probably be beyond your course.

Categories