How to get input from the user in java?

By Faruque Ahamed Mollick

Here in this Java tutorial, you will learn how to get input from users with simple example code snippets.

To get input from users in Java, we are going to use Java Scanner class. The Scanner class of Java is present in java.util package. To use the Scanner class, you just need to import the java.util.Scanner class. To do it, use the below line of code at the top of your code:



java.util.Scanner

First, we need to create an object of Scanner class. Below is the code of how to create an object of Scanner class:

Scanner a = new Scanner(System.in);

In the above line of code, a is the name of our object and new keyword used to allocate the memory and System.in is the input stream.

How To Read File From URL In Java?

Reading A Text File Line By Line In Java With Example

In this example code snippets, we are going to take an integer, a string and a float from the user by using Java Scanner class and then print it on the screen.

Below is the complete code:

import java.util.Scanner;
 
class UserInput {
   public static void main(String args[]) {
      int x;
      float y;
      String str;
 
      Scanner sc = new Scanner(System.in);
 
      System.out.println("Enter a string");
      str = sc.nextLine();
      System.out.println("The string you entered is "+str);
 
      System.out.println("Enter an integer");
      x = sc.nextInt();
      System.out.println("The integer you entered is "+x);
 
      System.out.println("Enter a float");
      y = sc.nextFloat();
      System.out.println("The float you entered is "+y);   
   }
}

In the above code, we have used three methods of Scanner class.

  1. To get an integer from a user we have used nextInt() method.
  2. To get a float from a user we have used nextFloat() method.
  3. To get a string from a user as input, we have used nextLine() method of the Scanner class.

There are some other types of methods available to take others data type as input.

Java Garbage Collection

Now run the above code snippets and you will get the output like you can see below:

First, it will ask you to enter a string:

Enter a string

Suppose you have entered a string “This is a String”. It will return you:

Enter a string
The string you entered is This is a String

Similarly, it will ask to enter an integer and after you enter an integer it will ask you to enter a float.Suppose you have entered 25 as integer and 11.2 as

Suppose you have entered 25 as integer and 11.2 as a float. Then it will give you the output that you can see below:

Enter a string
The string you entered is This is a String

Enter an integer
The integer you entered is 25

Enter a float
The float you entered is  11.2

There are also other classes available for taking input from users. For different devices and software developing environment, there are different inbuilt classes available to take input from users.

Leave a Reply

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