Java User Input

Java is a popular programming language that provides multiple ways to take input from users.

In this guide, we will discuss the most common methods for taking user input in Java.

Scanner Class

The Scanner class is one of the most commonly used classes for taking input from the user in Java.

The Scanner class is present in the java.util package, and it provides the nextLine() method, which is used to read the user input as a string.

Here is an example of how to use the Scanner class to take user input:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter your name: ");
        String name = sc.nextLine();
        System.out.println("Hello, " + name + "!");
        sc.close();
    }
}

BufferedReader Class

Another method for taking user input in Java is the BufferedReader class, which is present in the java.io package.

The BufferedReader class provides the readLine() method, which is used to read a line of text from the user.

Here is an example of how to use the BufferedReader class to take user input:

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

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter your name: ");
        String name = br.readLine();
        System.out.println("Hello, " + name + "!");
        br.close();
    }
}

Data Types

In Java, there are several data types that can be used to take user input, such as int, double, float, and char.

The Scanner and BufferedReader classes provide methods for taking input for all these data types.

Here is an example of how to use the Scanner class to take input for different data types:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter an integer: ");
        int num1 = sc.nextInt();
        System.out.print("Enter a double: ");
        double num2 = sc.nextDouble();
        System.out.print("Enter a character: ");
        char ch = sc.next().charAt(0);
        System.out.println("You entered: " + num1 + ", " + num2 + ", " + ch);
        sc.close();
    }
}

Exception Handling

When taking user input in Java, it is important to handle exceptions, such as when the user enters an invalid input.

The Scanner class throws an InputMismatchException when the user enters an invalid input, and the BufferedReader class throws an IOException.

Here is an example of how to handle exceptions when taking user input with the Scanner class:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter an integer: ");
        try {
            int num = sc.nextInt();
            System.out.println("You entered: " + num);
        } catch (Exception e) {
            System.out.println("Invalid input. Please enter an integer.");
        }
        sc.close();
    }
}

Conclusion

In conclusion, there are several methods for taking user input in Java, including the Scanner class and the BufferedReader class.

Both of these classes provide methods for taking input for different data types and it is important to handle exceptions when taking user input to ensure the program runs smoothly.

Java Basics