All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

How To Take String Input In Java Using Scanner Class

Last Updated : Mar 11, 2024

How To Take String Input In Java Using Scanner Class

In this article we will show you the solution of how to take string input in java using scanner class, popular programming language Java is commonly used to develop various programmes of all kinds.

Taking user input is one of the most typical requirements in Java development. Taking string input in particular is a regular requirement in many programmes.

The Scanner class from the java.util package can be used in Java to accept user-provided string input.

A file, a string, the console, and other sources are just a few of the ways the Scanner class can read data.

Now let's discuss the idea of using the Java scanner class to accept string input.

Step By Step Guide On How To Take String Input In Java Using Scanner Class :-

Moreover, there are additional methods in the scanner class which may be utilised for reading string input from several sources, such a file or a string.

NextLine(), NextByte(), NextShort(), NextInt(), NextLong(), NextFloat(), and NextDouble() are some of these functions.

import java.util.*;
class InputFromUser
public static void main(String[] args)
Scanner sc= new Scanner(;
System.out.print("Enter a string which you want: ");
String str=;
System.out.print("You have entered the string named as: "+str);
  1. As you can see, we created a straightforward Java programme that uses the Scanner class to capture user input and output it back to the terminal.
  2. The java.util package, which has a number of utility class, which include the Scanner class, is imported in the first line of the code. This class is used to read user input from the console.
  3. InputFromUser is a class that is declared in the second line of the code that follows.
  4. Every Java programme must contain at least one class, & the class name and file name must coincide.
  5. A main method inside the class acts as the application's entry point.
  6. When a Java programme runs, a unique method called public static void main(String[] args) is automatically called.
  7. The Scanner class is then recreated and assigned to variable sc in the next line.
  8. The Scanner class is instructed to read input from console by the parameter.
  9. The user is then prompted to input a string in a message that is printed to the console using the System.out.print() method.
  10. A user-provided string input is kept in the str variable and is read using the method.
  11. Finally, the programme uses the System.out.print() method to print the user input back to its console.
  12. You have entered the string named as: is concatenated with a message that states "You have entered the string named as:" to create the message.

Conclusion :-

So, utilising the scanner class, we have successfully mastered the Java idea of taking string input.

Additionally, we learned how to print messages to a console by using the System.out.print() function as well as how to read information from the console using the Scanner class.

It is a straightforward illustration of how to communicate with people through Java's command line interface.

I hope this article on how to take string input in java using scanner class helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪