Java - User Input - Scanner

In Java, user input can be taken through various input methods such as the Scanner class, BufferedReader class, and Console class. These classes provide different methods to read input from the user and process it accordingly. 

In Java, user input can be taken through various input methods such as the Scanner class, BufferedReader class, and Console class. These classes provide different methods to read input from the user and process it accordingly. Here is an example of how to take user input 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 your name: ");
      String name = scanner.nextLine();

      System.out.print("Enter your age: ");
      int age = scanner.nextInt();

      System.out.println("Hello, " + name + ". You are " + age + " years old.");
   }
}

In this example, the Scanner class is used to read input from the user. The System.in parameter is passed to the Scanner constructor to indicate that input is coming from the standard input stream (i.e. the console).

The nextLine() method is used to read a line of text input, while nextInt() method is used to read integer input. Once the user inputs their name and age, the program outputs a personalized greeting message.

It is important to remember to handle exceptions when using user input in Java. For example, if the user enters a non-integer value when prompted for age, an InputMismatchException will be thrown. Therefore, it is recommended to use a try-catch block to handle any potential exceptions that may occur when reading user input.

In Java, the Scanner class is used for taking input from the user. It provides various methods to read different types of input values. Here are the commonly used input types using Scanner in Java:

Integer input:

To read an integer value from the user, we can use the nextInt() method of the Scanner class. This method reads the next integer value from the user input.

Scanner sc = new Scanner(System.in);
System.out.println("Enter an integer value:");
int num = sc.nextInt();
System.out.println("The entered number is: " + num);

Floating-point input:

To read a floating-point value from the user, we can use the nextFloat() or nextDouble() method of the Scanner class. The nextFloat() method reads the next floating-point value as a float data type, while the nextDouble() method reads the next floating-point value as a double data type.

Scanner sc = new Scanner(System.in);
System.out.println("Enter a floating-point value:");
float num = sc.nextFloat();
System.out.println("The entered number is: " + num);

String input:

To read a string value from the user, we can use the next() or nextLine() method of the Scanner class. The next() method reads the next token (a sequence of characters separated by space) as a String data type, while the nextLine() method reads the entire line as a String data type.

Scanner sc = new Scanner(System.in);
System.out.println("Enter a string value:");
String str = sc.nextLine();
System.out.println("The entered string is: " + str);

Character input:

To read a single character from the user, we can use the next().charAt(0) method of the Scanner class. This method reads the next token as a String data type and returns the character at the specified index (0 in this case).

Scanner sc = new Scanner(System.in);
System.out.println("Enter a character:");
char ch = sc.next().charAt(0);
System.out.println("The entered character is: " + ch);

These are the common input types that can be used using Scanner in Java.