So far we were working on pre-initialized data. But real life application also requires different inputs from the application user.
In order to take user input, we use Scanner class in Java.
But, before using Scanner class, we need to include java.util package. java.util package contains the Scanner class with all its required methods to be used while writing program for taking user input.
First, you need to create an object of the Scanner class.
After that you can use all the methods available inside the Scanner class to take different form of user input.
Scanner Class Methods in Java (Input Types):
Methods | Uses of the Method |
nextLine() | This method is used to read a String value |
nextChar() | This method is used to read a Character value |
nextBoolean() | This method is used to read a Boolean value |
nextInt() | This method is used to read a Integer value |
nextShort() | This method is used to read a Short value |
nextLong() | This method is used to read a Long value |
nextFloat() | This method is used to read a Float value |
nextDouble() | This method is used to read a Double value |
Example of User Input:
1) Simple Interest Calculation
package userInput;
import java.util.Scanner;
public class ScannerUserInput {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the principal amount:");
int principal = sc.nextInt();
System.out.println("Enter the rate of interest:");
float rate = sc.nextFloat();
System.out.println("No. of Years:");
int time = sc.nextInt();
float simpleInterest = principal * rate * time / 100;
System.out.println("The simple interest is "+simpleInterest);
}
}
Output:
Enter the principal amount:
10000
Enter the rate of interest:
10
No. of Years:
1
The simple interest is 1000.0
2) User Input - String Value:
package userInput;
import java.util.Scanner;
public class ScannerUserInput {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// int principal = sc.nextInt();
System.out.println("Enter your first name:");
String firstname = sc.nextLine();
System.out.println("Enter your last name:");
String lastname = sc.nextLine();
System.out.println("Your full name is "+firstname+" "+lastname);
}
}
Output:
Enter your first name:
Rima
Enter your last name:
Saha
Your full name is Rima Saha
3) Simple Billing Application for Ration Shop:
package userInput;
import java.util.Scanner;
public class ScannerUserInput {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Heading
System.out.println("===================");
System.out.println("E-Ration Shop Billing");
System.out.println("===================");
System.out.println(" ");
// Ration Rate List
int ricerate = 10;
int sugarrate = 15;
int koilrate = 7;
// User Input
System.out.println("Rice (KG):");
int rice = sc.nextInt();
System.out.println("Sugar (KG):");
int sugar = sc.nextInt();
System.out.println("Kerosene Oil (lt.):");
int koil = sc.nextInt();
// Total price calculation
int ricecost = ricerate * rice;
int sugarcost = sugarrate * sugar;
int koilcost = koilrate * koil;
int total = ricecost + sugarcost + koilcost;
System.out.println(" ");
System.out.println("===================");
// Billing
System.out.println("Print Bill");
System.out.println("===================");
System.out.println(" ");
System.out.println("Rice ..... ("+rice+" kg x Rs.10/kg) = Rs."+ricecost+"/-");
System.out.println("Sugar ..... ("+sugar+" kg x Rs.15/kg) = Rs."+sugarcost+"/-");
System.out.println("Kerosene Oil ..... ("+koil+" lt x Rs.7/lt.) = Rs."+koilcost+"/-");
System.out.println(" ");
System.out.println("===================");
System.out.println("Total billing amount is Rs."+total+"/-");
System.out.println("===================");
System.out.println(" ");
System.out.println(" ");
System.out.println("THANK YOU");
System.out.println("E-RATION SHOP");
}
}
Output:
===================
E-Ration Shop Billing
===================
Rice (KG):
35
Sugar (KG):
10
Kerosene Oil (lt.):
8
===================
Print Bill
===================
Rice ..... (35 kg x Rs.10/kg) = Rs.350/-
Sugar ..... (10 kg x Rs.15/kg) = Rs.150/-
Kerosene Oil ..... (8 lt x Rs.7/lt.) = Rs.56/-
===================
Total billing amount is Rs.556/-
===================
THANK YOU
E-RATION SHOP
Comments
Post a Comment