-4

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.

Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69
Zeros
  • 1
  • What exactly have you tried? Have you read the relevant documentation? Please be specific. Please also include code as text, [not as an image](https://meta.stackoverflow.com/a/285557/10601203). Include any errors you may be encountering in the question. Here's a full guide on [how to ask](https://stackoverflow.com/help/how-to-ask). https://idownvotedbecau.se/itsnotworking/ https://idownvotedbecau.se/imageofcode https://idownvotedbecau.se/noresearch/ – Jesse Jun 25 '22 at 16:23
  • As has already been mentioned, please read the [ask] and [help] links and check out the [tour] to see how to best use this site. More importantly, while answers are telling you to "close the Scanner" object, do read [this important warning found in a comment in the duplicate](https://stackoverflow.com/questions/12519335/resource-leak-in-is-never-closed#comment121741965_12519335). – Hovercraft Full Of Eels Jun 25 '22 at 16:35

1 Answers1

0

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