How to Take String Input In Java using Scanner Class

How to Take String Input In Java using Scanner Class | In Java, to take input from the end-user, most of the time we use the Scanner class. Scanner class was introduced in Java 1.5 version. While taking input from the end-user, String is one of the most regularly used values to take input for the program. In this post, we will see how to take string input in java using the scanner class. See more:- String Programs In Java.

There are two different options:- read one word or read one line. In the Scanner class, we have the next() method which reads one word at a time and nextLine() method which reads one line at a time.

MethodUses
next()Read one word at a time.
nextLine()Read one line at a time.

While reading string value generally we prefer to read one line therefore we will focus on the nextLine() method. The nextLine() method is declared as follows:- public String nextLine()

Java Program to Take String Input in Java using Scanner Class

Steps to be followed to Take String Input In Java using Scanner Class:-

a) Import Scanner class. The Scanner class is defined in java.util package.
b) Create the Scanner class object. In this program, “scan” is a Scanner class object.
c) Declare a variable of string type to hold the input value. In this program, we created a “name” variable of String type.

d) Call nextLine() method on the Scanner class object “scan” to take string input.
e) Store it in the variable, and use them throughout the program.

import java.util.Scanner;

public class Test {

    public static void main(String[] args) {
        // create Scanner class object
        Scanner scan = new Scanner(System.in);

        // read input
        System.out.print("Enter Name: ");
        String name = scan.nextLine();

        // display
        System.out.println("Entered value: " + name);
        scan.close();
    }

}

The output of the above program:-

Enter Name: Know Program
Entered value: Know Program

The nextLine() method read one line at a time. For example:- the string literal “Know Program” contains two different words “Know”, and “Program” but since nextLine() read one line at a time, therefore, we are getting “Know Program”.

In this program, we read only one string but now let us see another program to read more string values from the end-user through the Scanner class.

Java program to Take String Input In Java using Scanner Class

import java.util.Scanner;

public class Test {

    public static void main(String[] args) {
        // create Scanner class object
        Scanner scan = new Scanner(System.in);

        // read input
        System.out.print("Enter Name: ");
        String name = scan.nextLine();

        System.out.print("Enter programming langauge: ");
        String lang = scan.nextLine();

        System.out.println("Enter 5 other language name:");
        String str[] = new String[5];
        for (int i = 0; i < 5; i++) {
            str[i] = scan.nextLine();
        }

        // display
        System.out.println();
        System.out.println("Name: " + name);
        System.out.println("Current programming language: " + lang);
        System.out.println("Other languages are:");
        for (int i = 0; i < 5; i++) {
            System.out.println(str[i]);
        }
        scan.close();
    }
}

The output of the above program:-

Enter Name: Rocco
Enter programming langauge: Java
Enter 5 other language name:
C
C++
Python
PHP
JavaScript

Name: Rocco
Current programming language: Java
Other languages are:
C
C++
Python
PHP
JavaScript

This program is very similar to the previous program but in this program, we have also taken a String array to store multiple string values using the Scanner class.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *