Reading and Printing a String from Input in Java

Learn how to read and print a string from user input in Java using the Scanner class with a simple, beginner-friendly example program.

Brief Introduction

Reading and Printing a String from Input in Java (java)
DESCRIPTION:
This example shows how to read a string from user input via console and print it on screen using Java. It is a simple and complete program, suitable for beginners.

ADDITIONAL NOTES:
The program uses the Scanner class to read input from the keyboard. It is important to close the scanner after use to avoid resource leaks.

EXPLANATIONS:
- import java.util.Scanner; - Imports the Scanner class needed to read input from the console.
- Scanner scanner = new Scanner(System.in); - Creates a Scanner object that reads from standard input (keyboard).
- System.out.print(...) - Prints a message without a newline, to prompt the user to enter data.
- scanner.nextLine() - Reads a full line of text entered by the user.
- System.out.println(...) - Prints the read string on screen, with an explanatory message.
- scanner.close() - Closes the scanner to free system resources.

Example Code: java

import java.util.Scanner;

/**
 * Simple program to read a string from input and print it.
 */
public class LetturaStringa {
    public static void main(String[] args) {
        // Creation of a Scanner object to read from console
        Scanner scanner = new Scanner(System.in);
        
        // Prompting the user to enter a string
        System.out.print("Inserisci una stringa: ");
        
        // Reading the line entered by the user
        String inputUtente = scanner.nextLine();
        
        // Printing the read string
        System.out.println("Hai inserito: " + inputUtente);
        
        // Closing the scanner to free resources
        scanner.close();
    }
}
×
×
×
Do you want to regenerate the answer?
×
Export chat
Choose export format
Generazione PDF in corso…
Allegati
×
You are about to close the chat and switch to the image generator. If you are not logged in, you will lose our chat. Do you confirm?
You are viewing a shared chat in temporary mode. It will not be saved.
×
Saved prompts
×
Private note
×
Label
×
Search all chats
×
Your insights
Analyzing…
×
AI Preferences
Assistant name 30
Answer length
AI model
Structured answers
Include examples
Use emojis
Guide me, don't solve Hints and questions instead of a ready answer
More settings →
×
Share this chat
Anyone opening this link can view the chat or add it to their profile as their own chat.
Note: the chat attachments will be shared too. Anyone who adds it will receive a copy of the files in their own folder.
Shared chat
Someone shared a chat with you. Do you want to just view it or add it to your chats?
Note: the chat attachments will be shared too. Anyone who adds it will receive a copy of the files in their own folder.
×

Saved messages

Loading...

×

Chat History

example-programs · CHAT HISTORY

Loading...

AI Preferences

×
  • BasicQuick and essential answers for study
  • MediumHigher quality for study and programming
  • AdvancedComplex reasoning and detailed analysis
Explain Steps

What do you need?

Java Development Kit (JDK) installed

Development environment or terminal to run Java programs

DEPENDENCIES: no external ones, uses only standard Java libraries

CONFIG: no configuration required