Write a Program that Basic input using Scanner class in Java
Write a java program to perform basic input using Scanner class. In this article I will explain how to input all basic data types using Scanner class in Java. We will learn how to use java.util.Scanner class to input data from user.
For Executing any java program, you need to
- Install the JDK if you don't have installed it, download the JDK and install it.
- Set path of the jdk/bin directory.
- Create the java program
- Compile and run the java program
Example
Input
Enter charater value: c
Enter integer value: 10
Output
You entered charater : c
You entered integer: 10
In previous post we learned basics of Java Programs. In this example we will continue further and learn basic input output using Scanner class in Java.How to read input using Scanner class
Java has a built-in Scanner class, to perform basic input output on all primitive data types. It is defined under java.util package.Scanner class provides methods to read input of all primitive data types. It uses regular expressions to break its inputs into tokens.
How to use Scanner class
Import Scanner class at the top of your Java program. We must import all classes that we use in our Java program. Hence write an import statement at top, say import java.util.Scanner;.Create an object of Scanner class. Say Scanner sc = new Scanner(System.in);.
Use Scanner class methods as per your data type to read input from user. Say to read integer from user use sc.nextInt();, similarly use others.
Scanner class input methods
- next() - Finds and returns the next string from input stream.
- nextLine() - Return line from from input.
- nextByte() - Return byte read from input.
- nextShort() - Return short read from input.
- nextInt() - Return integer read from input.
- nextLong() - Return long read from input.
- nextFloat() - Return float read from input.
- nextDouble() - Return double read from input.
Program to read input using Scanner class
// Java program to read input using Scanner class
import java.util.Scanner;
public class BasicScannerIO {
public static void main(String[] args) {
// Declare all basic data types variable
// for character type
char charVar;
// for integer type
byte byteVar;
short shortVar;
int intVar;
long longVar;
// for floating type
float floatVar;
double doubleVar;
// for string type
String strVar;
/* Create Scanner object */
Scanner sc = new Scanner(System.in);
System.out.print("Enter character value: ");
charVar = sc.next().charAt(0);
System.out.print("Enter byte value: ");
byteVar = sc.nextByte();
System.out.print("Enter short value: ");
shortVar = sc.nextShort();
System.out.print("Enter integer value: ");
intVar = sc.nextInt();
System.out.print("Enter long value: ");
longVar = sc.nextLong();
System.out.print("Enter float value: ");
floatVar = sc.nextFloat();
System.out.print("Enter double value: ");
doubleVar = sc.nextDouble();
sc.nextLine(); // Skip extra newline character
System.out.print("Enter a String: ");
strVar = sc.nextLine();
sc.close(); // Close input stream (Scanner)
/*
* Print value of all variable
*/
System.out.println("You entered character: " + charVar);
System.out.println("You entered byte: " + byteVar);
System.out.println("You entered short: " + shortVar);
System.out.println("You entered integer: " + intVar);
System.out.println("You entered long: " + longVar);
System.out.println("You entered float: " + floatVar);
System.out.println("You entered double: " + doubleVar);
System.out.println("You entered String: " + strVar);
}
}
If you have copied the exact code, you need save the file name as BasicScannerIO.java. It's because the name of the class and filename should match in Java.
Compile: javac BasicScannerIO.java
Execute: java BasicScannerIO
Note: System.out.print() method print string in same line. Whereas System.out.println() method print string and moves cursor to new line.
You might be wondering why I have used extra sc.nextLine();. This is because nextDouble() read next double value from input stream ignoring the new line character. Hence to eat that new line character I have used a dummy nextLine() function.
You May Also Like:
Sum of Two and Three Numbers in java
Subtract of Two and Three Numbers in java
Multiplication of Two and Three Numbers in java
That’s it. Now you have successfully print Basic input using Scanner class Program in Java. If you have any doubt or question comment down below.
I hope this post will useful to you, Thanks For Visiting, Keep Visiting.
No comments