Input in Java Programming
Input and Output in Java
In this lesson, we will understand what is Scanner class, and how it is used in the Java program to accept input with the help of some examples.
Input in Java
The Scanner class is used in the Java program to accept input from the standard input device (keyboard) and store them in one or more variables. The Scanner class is available in java.util package.
To use the Scanner class, we need to create an object of the Scanner class and use the available functions found in the Scanner class to take the different types of input from the user. The available functions in Scanner class are given below.
Method | Description |
nextBoolean() | Reads a boolean type value from the user |
nextByte() | Reads a byte type value from the user |
nextDouble() | Reads a double type value from the user |
nextFloat() | Reads a float type value from the user |
nextInt() | Reads an int type value from the user |
nextLong() | Reads a long type value from the user |
nextShort() | Reads a short type value from the user |
nextLine() | Reads a String type value from the user |
Let's see some examples for more understanding.
Example 1
Java program to input a character and a string and store the values in the variables.
import java.util.Scanner;
public class Example
{
public static void main(String args[])
{
char a;
String str;
Scanner sc=new Scanner(System.in);
System.out.println("Enter any character ");
a=sc.nextLine().charAt(0);
System.out.println("Enter any string ");
str=sc.nextLine();
System.out.println("You have entered "+a);
System.out.println("You have entered "+str);
}
}
Output
Enter any character b Enter any string Hello World You have entered b You have entered Hello World
Here you can see that in line number 7 and 8 we have declared 2 variables, a char variable a and a String variable str. On line number 9 we have created an object sc of Scanner class. You can use any name instead of sc but remember to follow the variable naming rules that we have discussed earlier.
On line number 11 we are taking a character input from the user. charAt(0) function is used along with the nextLine() function to read a single character from the user input. On line number 13 we are taking a string input from the user using the nextLine() function.
After taking all the inputs from the user on line number 14 and 15 we are printing the values of the variable a and str on the screen.
Example 2
Java program to input and store an integer number, a float number and a double type number in three variables.
import java.util.Scanner;
public class Example
{
public static void main(String args[])
{
int a;
float b;
double c;
Scanner sc=new Scanner(System.in);
System.out.println("Enter an integer, a float and a double type numbers");
a=sc.nextInt();
b=sc.nextFloat();
c=sc.nextDouble();
System.out.println("You have entered an integer number "+ a +", a float number "+ b +" and a double type number "+c);
}
}
Output
Enter an integer, a float and a double type numbers 18 54.2365 91.16249 You have entered an integer number 18, a float number 54.2365 and a double type number 91.16249
You can see that we have used nextInt(), nextFloat() and nextDouble() function to accept an integer, a float and a double type numbers from the keyboard and store in the variables a,b and c respectively.