In Java, you can use the Scanner
class to read input from the user. The Scanner
class provides methods for reading various data types, such as strings, doubles, and integers, from the standard input stream.
Here's an example of how to use the Scanner
class to read a string, a double, and an int value from the user:
Scanner scanner = new Scanner(System.in); System.out.print("Enter a string: "); String str = scanner.nextLine(); System.out.print("Enter a double: "); double d = scanner.nextDouble(); System.out.print("Enter an int: "); int i = scanner.nextInt();
In this example, we're using the nextLine
method to read a string from the user, the nextDouble
method to read a double from the user, and the nextInt
method to read an int from the user.
The Scanner
class reads the input from the standard input stream (System.in
) and separates it into tokens using a delimiter pattern. By default, the delimiter pattern is any sequence of whitespace characters.
You can find more information about the Scanner
class and how to use it to read input from the user in Java in the Java documentation.