Java Java Tutorial Java Methods Java Classes Java File Handling Java Reference



Java User Input

Java is a popular programming language that is widely used for developing various applications. One of the important features of Java is its ability to accept user input. Java user input allows users to interact with the program and provide input data that can be processed by the program. In this article, we will discuss Java user input in detail.

Brief Explanation of Java User Input

Java user input is the process of accepting input data from the user during the execution of a Java program. The input data can be of various types such as numbers, strings, characters, and so on. Java provides several classes and methods to accept user input. The most commonly used classes for accepting user input are Scanner and BufferedReader.

The Scanner class is used to read input data from the user. It provides various methods to read different types of input data such as nextInt(), nextDouble(), nextLine(), and so on. The BufferedReader class is used to read input data from the user as a string. It provides the readLine() method to read input data as a string.

Code Examples

Let's take a look at some code examples to understand how to accept user input in Java.

Example 1: Using Scanner Class

The following code snippet shows how to accept an integer input from the user using the Scanner class.


import java.util.Scanner;

public class UserInputExample {
   public static void main(String[] args) {
      Scanner scanner = new Scanner(System.in);
      System.out.print("Enter an integer: ");
      int num = scanner.nextInt();
      System.out.println("You entered " + num);
   }
}

In the above code, we first create an object of the Scanner class and pass the System.in object as a parameter to the constructor. The System.in object represents the standard input stream which is the keyboard in this case. We then use the nextInt() method of the Scanner class to read an integer input from the user. Finally, we print the input value using the println() method.

Example 2: Using BufferedReader Class

The following code snippet shows how to accept a string input from the user using the BufferedReader class.


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class UserInputExample {
   public static void main(String[] args) throws IOException {
      BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
      System.out.print("Enter a string: ");
      String str = reader.readLine();
      System.out.println("You entered " + str);
   }
}

In the above code, we first create an object of the BufferedReader class and pass the System.in object as a parameter to the InputStreamReader constructor. We then use the readLine() method of the BufferedReader class to read a string input from the user. Finally, we print the input value using the println() method.

Conclusion

Java user input is an important feature that allows users to interact with the program and provide input data that can be processed by the program. Java provides several classes and methods to accept user input such as Scanner and BufferedReader. By using these classes and methods, we can easily accept user input in our Java programs.

References

Activity