All TalkersCode Topics

Follow TalkersCode On Social Media

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

How To Take Input From User In Java

Last Updated : Mar 11, 2024

How To Take Input From User In Java

In this article we will show you the solution of how to take input from user in java, Java is widely used as a programming language due to its versatility, efficiency and scalability.

It is a very popular object-oriented language for creating web, desktop and mobile applications.

Regarding user input, Java provides different ways to receive input from the user that you can use depending on the needs of your application.

Some of the most popular techniques for receiving user input in Java are covered in this article.

We will cover the following methods for receiving input from the user in Java:

Using the Scanner class

Using the BufferedReader class

Step By Step Guide On How To Take Input From User In Java :-

import java.util.Scanner;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class UserIp {
   public static void main(String[] args) {
      Scanner scanner = new Scanner(System.in);
      BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
      // code to take input using Scanner class
      System.out.println("Please Enter Name of Your Favourite Food :");
      String food = scanner.nextLine();
      System.out.println("Your Favourite Food is : " + food);
      // code to take input using BufferedReader class
      try {
         System.out.println("Enter your Favourite Sports :");
         int sports = Integer.parseInt(reader.readLine());
         System.out.println("Your Favourite sports is : " + sports);
      } catch (Exception e) {
         System.out.println("Invalid input");
      }
   }
}
  1. First, import the Scanner and BufferedReader classes from the java.util and java.io packages, respectively.
  2. In the main method, create instances of the Scanner and BufferedReader classes.
  3. To read a line of text from the user and store it in the name variable, we will use the nextLine() function of the Scanner class.
  4. And to read a line of user text using the BufferedReader class's readLine() function, and then we perform the Integer class' parseInt() method to convert it to an integer.
  5. If the user enters invalid input, an exception is thrown, the catch block is executed, and an error message is printed.

Conclusion :-

In this tutorial, we have covered two common methods of receiving input from the user in Java: the Scanner class and the BufferedReader class.

Scanner class is used to read input in the form of text or basic data types and BufferedReader class is used to read input in the form of text lines.

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

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪