How do I get input directly from keyboard? [duplicate] - java

This question already has answers here:
How to get the user input in Java?
(29 answers)
Closed 6 years ago.
I am an AP Computer Science student who is trying to write a console-based text editor. My teacher taught me how to read input using Scanner(System.in), but in a text-editor, you need to get input directly from the keyboard. How do I gain access to keystrokes without using the Scanner class, or a similar parallel? For example, BufferReader(new InputStreamReader(System.in))
EDIT: My point was how read without waiting for user to press enter (otherwise users couldn't add text in real time)
EDIT: Why are you saying my question is a duplicate of that question? I explicitly asked for a method of getting input without waiting fo ruser to press enter. The answers to that other question do not resolve that at all!

You can use this :
System.out.print("Please enter your String :\t");
String sentence = scanner.nextLine();

Related

What is wrong here? I cannot run user input code [duplicate]

This question already has answers here:
Resource leak: 'in' is never closed
(14 answers)
Closed 8 months ago.
enter image description here
I just started learning java today. I am now stuck in this user input coding. I have tried many methods but I cannot run this user input code.
As the others already stated closing the scanner is best practice.
If you use Java 8 or higher you can do it the following way:
try(Scanner sc = new Scanner(System.in))
{
String name = sc.nextLine();
System.out.println(name);
}
Besides that your code already works! When you run the program it's waiting for input, so you need to write some text into the console-Window and press enter

How to put a certain string into *if* and check if it is a certain word [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 4 years ago.
I am trying to scan a string and put it in an if like so:
(this is just an example part of the whole program)
Scanner scan=new Scanner();
System.out.print("Enter the word...");
String a=scan.nextLine();
if (a=="Hi")
System.out.println("Hello!")
Else System.exit(0);
So im checking if the user said "Hi" , but it doesnt work like this, I want a way to do it.
I have just started learning java so my questions might seem a little amateur but your answers will help a lot.
Use "Hi".equals(a), == compares identity, not equality.
Also, you should write specifically "Hi".equals(a) and not a.equals("Hi"), because it won't cause a NullPointerException, since a might be null.

Changing a hardcoded object [duplicate]

This question already has answers here:
How can I read input from the console using the Scanner class in Java?
(17 answers)
Closed 6 years ago.
I have tried googling this, and looked for a while, but have not found anywhere that explains how to do this well
SalariedEmployee salariedEmployee =
new SalariedEmployee("bob", "Smith", "222-22-2222", "800");
The variables are firstname, lastname, social security number, and weekly salary.
How would i change that to something where i can ask what each of the variables should be such as
SalariedEmployee salariedEmployee =
new SalariedEmployee(firstName, lastName, ssn, salary);
and then ask
what is the employee's first name?
what is the employee's last name?
What is the employee's ssn?
What is the employee's salary?
If anyone can explain this, or has seen this explained somewhere else your input would be greatly appreciated.
Read inputs from a user using a Scanner object or a Buffered Reader and save them locally. Initialize the object after you have all the inputs. Alternatively you can have set methods and assign them in any order you like.

Java - Use Users input in code [duplicate]

This question already has answers here:
How to get the user input in Java?
(29 answers)
Closed 8 years ago.
I am trying to build a program that talks to the user and I want it to know as much as possible. For that I need an ability to use what the user types in my programs code and not just in variables in a way that it is fully implemanted in to the code
Or if there is now way, you know of just Tell me and I will accept it.
The Scanner class can be used for input. Example:
Scanner scanner = new Scanner(System.in);
String age = "";
System.out.println("Please enter your age:");
age = scanner.next();
This code will get user input via the console.

Eclipse - Output character different from Keyboard Key Value [duplicate]

This question already has answers here:
Why is my keyboard messed up in Eclipse?
(10 answers)
Closed 3 years ago.
I am having a strange problem with Eclipse. The problem is that
For some keyboard keys, the output value printed on screen is different from actual value of that key. e.g. When I press # key, it prints £ sign instead. Similarly when i try quotes key " , the # is printed instead.
Outside the eclipse, the keys work fine.
I think u should change your keyboard settings. It may be in any other english version type.

Categories

Resources