JAVA · ОСНОВЫ · LESSON 10

User response

Scanner and input · about 17 minutes

Learning with progressRead the summary for free. To save the work, log in to the account.
STEP 1

Ask the name.

Scanner reads what a person has printed. Before the Main class, add import, and inside the main, create Scanner for System.in.

import java.util.Scanner;
Scanner input = new Scanner(System.in);

✦ import connects the finished tool Java.

STEP 2

Get the text.

nextLine is waiting for a whole line. Save it to String and show it to me. Close the Scanner after work.

String name = input.nextLine();
System.out.println("Привет, " + name);
input.close();
Talking.
keyboardwriterScannerreadingprintlnrespondent

✦ When the program runs on the computer, it will wait for the answer.

IN-CLASS PRACTICE

Type the code yourself

Reprint the example. The code does not run on the site; try it later in VS Code.

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String name = input.nextLine();
        System.out.println("Привет, " + name);
        input.close();
    }
}
Open the editor for class work
HOMEWORK

Solve it yourself

Connect Scanner. In main, read one line in the color variable and output «Цвет: » along with color. At the end, close the Scanner.

The answer is hidden. Use similar examples from the lesson notes.

Opening up your homework