How to get input from user in JavaJava Scanner ClassJava Scanner class allows the user to take input from the console. It belongs to java.util package. It is used to read the input of primitive types like int, double, long, short, float, and byte. It is the easiest way to read input in Java program. SyntaxThe above statement creates a constructor of the Scanner class having System.inM as an argument. It means it is going to read from the standard input stream of the program. The java.util package should be import while using Scanner class. It also converts the Bytes (from the input stream) into characters using the platform's default charset. Methods of Java Scanner ClassJava Scanner class provides the following methods to read different primitives types:
Example of integer input from user The following example allows user to read an integer form the System.in. Output: ![]() Example of String Input from user Let's see another example, in which we have taken string input. Output: ![]()
Next TopicJava Tutorial
|
JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week