focus on user input using Scanner class in java - 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();

Related

(JAVA) Read user input with out deleteing what they have written when sending System.out

This is in a Java command line application. I am trying to make an application that will be constantly receiving data and then printing it. I also need to accept commands to send out the port. Every time I type something it gets moved up a line when the console outputs. Ideally I could have a prompt before the command. I have tried to grab the current input, delete it, then print the msg, and then reprint the input, but I could only grab input after the user pressed enter. I have spent a couple hours just googling trying to find the answer. I also have asked some people I know but still nothing.
I tried
Scanner scn = new Scanner(System.in);
while(true) {
System.out.println(scn.next());
}
but it does not fire until I press enter.

Displaying a command prompt inside an infinite loop

I've a command line application which awaits an user input. I want to display > as the prompt.
This is my code.
while(true){
System.out.print("> ")
// do stuff
}
The problem is that the > displays every time the loop iterates but the cursor blinks at the beginning of the loop, thereby making my > a part of the user input. I want the cursor to be blinking one space after the > everytime the loop iterates and also > not to be a part of the next line object(assuming I'm using a scanner to parse input). Any help appreciated.
If you really worry about "great user experience"; than anything that only uses the "java builtin" console functions ... will not meet that requirement.
So, if that matters to you, I recommend looking into the various libraries that give you "ncurses like" functionality, like charva, java curses or lanterna.
I agree with #Aaron that "> " will not be part of the user's input, you can verify this by adding the lines below just under System.out.print("> ");
String usrInput = new java.util.Scanner(System.in).next();
System.out.println("Your input was " + usrInput);
If you are just asking how to change the while loop to make it blink after the > :
System.out.print(">_");
while(true) {
Thread.sleep(400);
System.out.print("\b ");
Thread.sleep(400);
System.out.print("\b_");
}
I wouldn't recommend to handle input this way, but it will get you the visual effect you are after.
When you use System.out.print or most other output commands, you're actually putting text in stdout / System.out, which your console will check to display said text.
It will not interfere with the user input which is transmitted via the independant stdin / System.in.
The problem is that these buffers are not modifiable, only appendable : once you've written something you can't come back to erase it, so making a prompt blink is not possible this way.
Some console might handle the BACKSPACE 0x08 ascii control character to erase the previous character, but I wouldn't rely on it.
There might be other methods that would grant you a read/write access to the console text buffer, but I doubt using it would be much easier than writing a GUI. I'd recommend either implementing your program in a language specific to your console (bash, powershell, etc.) or implementing a GUI.

how to pass input during executin of java code in eclipse

I pass command line arguments to my java code in eclipse (and retrieve it using args[] in main method)-this is fine.
However, my scenario is different. My code periodically asks for input during execution. Where would I enter such input? when controls hits such input prompt, eclipse freezes
EDIT
Some of the answers I read below suggests using command line args or buffered reader or using console view: but my eclipse freezes right after asking me for sudo password (further, my situation is different from command line args as explained in the very first line).
I am using Kepler on centos 6.5
Eclipse has a console, its typically located at the bottom of your java perspective. If its not there just go to Window->Show View->Console.
Have you ever used an input stream reader or a buffered reader?
The following is a start on how
static InputStreamReader input = new InputStreamReader(System.in);
static BufferedReader in = new BufferedReader(input);
Then you can use the buffered reader for taking input during execution.
you can pass command line arguments from eclipse, run your program from eclipse as run on configuration it will open dialog box and click arguments tab and pass your arguments from there
Go to Window -> Show View -> Console. You will able to enter input.
For short cut , type : Alt + Shift + Q, C.
Use the Scanner class. You have to import it from java.util.Scanner;
String userInput;
Scanner input = new Scanner(System.in);
System.out.print("Enter something: ");
userInput = input.nextLine();
and remember to close the scanner when you're done using it
input.close();
Puzzled why my question is down voted and why few respondents suggested irrelevant answer.
You can pass input using scanner or input stream if your code is expecting it. But my question is asking for uncertain situations(in this case, if my eclipse is started as sudo it wont ask for root password; but if I start as regular user it would ask for root password based on the Linux shell command that my java code has to exec)
Yes, you can pass the input to java code (executed under eclipse). From the command line window where eclipse was started, we can enter the input and java code can read it as string.

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.

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