All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Get String Input In Java

Last Updated : Mar 11, 2024

How To Get String Input In Java

In this article we will show you the solution of how to get string input in java, the package java_prgms will imported by eclipse default during creation of each java program at initial stage.

The utility class supports scanner class to take string data type input from end user. A successful scanner object will help you to collect input from user with few statement or property.

Step By Step Guide On How To Get String Input In Java :-

The scanner class is part of the java.util package and it was received from user then parse into primitive data types such as float, int, double or string.

Here in main class ‘StrInp_prgm’ we provide main method definition. Within that using next() method we achieve the result and displayed on terminal.

//GET CHARACTER INPUT FROM USER
package java_prgms;
import java.util.*;
class StrInp_prgm
{
 public static void main(String args[])
    {
        Scanner scobj=new Scanner(System.in);
  System.out.println("Enter String Input");
         String str=scobj.next();
  System.out.println("You input string is : "+str);
    }
}
  1. The ‘Package java_prgms’ may differ as per developer definition and its container name where your overall data stored it. Actually it’s a namespace that organizes a set of related classes and interfaces.
  2. The main class named as ‘StrInp_prgm’, there we provide static main method definition. The ‘public static void main(String[] args)’ is starting point from where compiler starts program execution.
  3. At main method, first you need to create scanner object ‘scobj’ as per inbuilt rule. Instead object ‘scobj’ you can modify it depends on your own thought.
  4. Then you have to provide any message that will notify user to enter input through text ‘Enter String Input’. After that you need to binds the scanner object with next() method to collect string input from end user.
  5. The next() method will returns the string which is user entered and the result value stored on variable ‘str’ and displayed final result online with text ‘Your input string is : Hi’ with the help of println() method.
  6. The ‘System.out.println()’ is used to print an argument that is passed to it. And which is separate into three parts such as System- final class in java, out-instance of PrintStream type that is a public and static member field of System class, println()-instance of PrintStream class hence we can invoke same on out.
  7. Ensure each brackets has closed with its pair perfectly at the end. Then every statement must end by ‘;’.

Conclusion :-

In conclusion now we are able to know how to take string input in java by scanner class.

To compile java program you need to tell the java compiler which program to compile through ‘javac StrInp_prgm.java’.

After successful compilation you need to execute ‘java StrInp_prgm‘ line for gets the output.

Then you can see output ' Enter String Input’ now user needs to enter input string that will displays as final result of text ‘Your input string is : Hi'.

On eclipse just press run button then you will get output inside terminal.

I hope this article on how to get string input in java 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 🡪