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

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

Related

Resource leak: 'sc' is never closedJava(536871799) [duplicate]

This question already has answers here:
Close a Scanner linked to System.in
(5 answers)
Scanner is never closed
(4 answers)
Closed 3 months ago.
sc function is never closed. What to do to avoid this error?
I tried to input integer data using scanner function in java, but the sc line is facing some error. Although the code runs properly. But I want to know why it showing error. Thanks in advance.

Input data using Scanner in Java [duplicate]

This question already has answers here:
What does Scanner input = new Scanner(System.in) actually mean?
(5 answers)
Closed 4 years ago.
Can anyone one tell what this statement exactly does?
Scanner input = new Scanner(System.in) ;
what is (input) a variable or an object.What is System.in?
Please someone tell me what is meaning of this whole statement
input
is a variable, more precisely an instance object of the class
Scanner
when you do
Scanner input = new Scanner(System.in);
you create a variable named input that is a Scanner instance and reads inputs from the standard console until you close it with
input.close();
note that you cannot reopen the instance if you close it, if you want to re-read inputs you have to recreate it with:
input = new Scanner(System.in);

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.

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

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();

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.

Categories

Resources