How to read input from the user in Java

This blog post demonstrates how to read a user’s input in Java.  You can use the following code snippet:

public class ReadInput {public static void main(String args[]){Scanner scanner = new Scanner(System.in);System.out.println("Enter your input:");int num = scanner.nextInt();System.out.println("The number entered is "+num);}}

 

This code uses the Scanner class. This class has a method called nextInt which is used to read an integer input. In addition to this method, it has several other methods like nextDouble, nextFloat ,etc which can be used to read other data types.  In addition, it also has a method called next, which can be used to read String data.

Comments

Popular posts from this blog

How to use logging in SpringBoot with code samples

Python While Loop with code samples

How to convert a List to a Set