User Input In Java – Scanner Class

Our topic today is Java User Input, and we will provide examples to ensure that the learners are able to better understand it.

Java User Input

As part of Java User Input, there is a Scanner class that is used to get user input. It can be found in the java.util package.

Creating an object of the Scanner class and calling one of the methods listed in the documentation will allow you to use the Scanner class.

Syntax:

Scanner mrx=new Scanner(System.in);

Here is an example of using the nextLine() method to read a string:

Example: 

import java.util.Scanner; // Scanner Package is added OR Scanner class is imported here public class Main { // Main Class body startspublic static void main(String[] args) {Scanner my_input=new Scanner(System.in); // Creating an Object of Scanner ClassSystem.out.println("Enter Your Gender here: "); String mrx_gender=my_input.nextLine(); // The input is taken in this line using Scanner Object and the value is stored in mrx_genderSystem.out.println("Gender Entered: "+mrx_gender); }// Output: Gender Entered : Male }

Multiple inputs can be taken from the user as shown in the example below:

Example: 

import java.util.Scanner; // Scanner Package is added OR Scanner class is imported here public class Main { // Main Class body startspublic static void main(String[] args) {Scanner num_input=new Scanner(System.in); // Creating an Object of Scanner ClassSystem.out.println("Enter First Number here: "); int mrx=num_input.nextInt(); // The input is taken in this line using Scanner Object and the value is stored in mrx_genderSystem.out.println("Enter Second Number here: "); int ample=num_input.nextInt();System.out.println("Product of Both Numbers is: "+mrx+" x "+ample+" = "+(mrx*ample)); }// Output: Product of Both Numbers is: 7 x 6 = 42 }

 

The Java Packages Tutorial gives you an overview of what a package is and what you can do with it.



Java Input Types

As shown in above examples, nextLine() was used to read strings and nextInt() was used to fetch int values. Check out the table below to discover more about other types:

MethodsOverview
BigDecimal nextBigDecimal()This function scans the next token as a BigDecimal.
BigInteger nextBigInteger()A BigInteger is used to scan the next input token.
nextBoolean()Obtains a boolean value from the user.
nextByte()Read a byte value from the user.
nextDouble()Receives a double value from the user.
nextFloat()Obtains a float value from the user.
nextInt()Obtains an int value from the user.
nextLine()Reads a String value from the user.
nextLong()Obtains a long value from the user.
nextShort()Reads a short value from the user.

 

Here are some examples of reading data of different types using different methods:

Example: 

import java.util.Scanner;// Following program takes all numeric input types; public class Main { public static void main(String[] args) {Scanner my_input=new Scanner(System.in);// Taking int inputSystem.out.println("Enter any integer here: "); int mrx_I_num=my_input.nextInt();// Taking float inputSystem.out.println("Enter any float value here: "); float mrx_f_num=my_input.nextFloat();// Taking double inputSystem.out.println("Enter any double value here: "); double mrx_d_num=my_input.nextDouble();// Taking long inputSystem.out.println("Enter any long value here: "); long mrx_l_num=my_input.nextLong();// Taking long inputSystem.out.println("Enter any short value here: "); short mrx_s_num=my_input.nextShort();// Printing the values below:System.out.println("\nThe values are: \n");System.out.println("Int value: "+mrx_I_num); System.out.println("Float value: "+mrx_f_num); System.out.println("Double value: "+mrx_d_num); System.out.println("Long value: "+mrx_l_num); System.out.println("Short value: "+mrx_s_num);}// Outputs: The values are: // Output 1: Int value: 1231234 // Output 2: Float value: 23.63424 // Output 3: Double value: 34.4234324523 // Output 4: Long value: 2344684567549582323 // Output 5: Short value: 32,763 }

Another approach.

Example: 

import java.util.Scanner;// Following program takes other Input types; public class Main { public static void main(String[] args) {Scanner my_input=new Scanner(System.in);// Taking String inputSystem.out.println("Enter name here: "); String mrx_Str_name = my_input.nextLine();// Taking byte inputSystem.out.println("Enter any Byte value here: "); byte mrx_Byte_num=my_input.nextByte();// Taking boolean inputSystem.out.println("Enter any Boolean value here: "); boolean mrx_Bool_val=my_input.nextBoolean();// Printing the values below:System.out.println("\nThe values are: \n");System.out.println("String name: "+mrx_Str_name); System.out.println("Byte value: "+mrx_Byte_num); System.out.println("Boolean Value: "+mrx_Bool_val);}// Outputs: The values are:// Output 1: String name: James Gosling // Output 2: Byte value: 76 // Output 3: Boolean Value: true }

Remember: You will receive an exception/error message (like “InputMismatchException“) if you enter incorrect input (e.g. text in a numerical input).

Java User Input is now familiar to you and you know how to use it – More information about exceptions and error handling can be found in our up coming chapter on Exceptions.

We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *