Getting an input with a java applet while executing a method - java

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?

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.

Exit java program using ctrl+z?

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?

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.

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.

focus on user input using Scanner class in java

When using a Scanner object is it possible to ensure the cursor always focuses in the console? For example when I run my program if I do not click in the console and start typing my input ends up in the middle of my program code if I am not paying attention.
Scanner s = new Scanner(System.in);
String name = s.nextLine();
System.out.print("Enter your name")
So when I run this program I will get Enter your name in the console and I would like to automatically focus the cursor ready to take users info at the end of the sentance.
Thanks for any help
No, it's not possible, at least not in general. Scanner has absolutely nothing to do with the console (it might be reading input from a socket or a file) and even if it is reading from the console, it cannot control your cursor location.
Something like java.awt.Robot would give you limited ability to control the cursor (via mouse clicks) but it would be very environment specific (what if the user moves the console? what if they're not even running in the IDE/from the command line?)
You're solving a problem that you don't need to solve, and at a level that shouldn't be cognizant of the problem (if anything, solve this in environment configuration, not in program logic).
As AKJ said, not focusing on the console, totally depends on IDE.
As for your code, it will first wait for the user input then user is prompted. Hence, you might interchange the second and third line of code.
Scanner s = new Scanner(System.in);
System.out.print("Enter your name");
String name = s.nextLine();

Categories