Input output string

Java has multiple ways to read input from the console. In this program, we are using the class Scanner to read input from the console.

Output String

You can directly print an string value to the console without specifying its data-type, however, Java also provides a way to print string data just like in C language.

See also: Java program to print to console

Method nextLine()

The method nextLine() of Scanner class reads the whole line from the console.

Program

import java.util.Scanner;

public class InputOutputString {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        System.out.println("Enter your name : ");

        // Read input
        String name = scanner.nextLine();

        System.out.println("Hello " + name);

    }
}

Output

Enter your name : Karan
Hello Karan