Skip to main content

Taking User Input in Java

  

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

Popular posts from this blog

What is Java

Java is a general purpose programming language. Which means this programming language is used in many purposes apart from being used only in android application development. There are a few similarities between Java programming language and C++ programming language. But Java programming language is having some advanced features which you are going to learn in future. In programming languages you write programs in the form of statements in many lines and those statements are executed one by one. In Java, the execution happens really fast and concurrently it can execute many statements. Java is a platform independent (Java compiled byte code can be run across different operating systems) and object oriented programming language.   Why should we learn Java programming language?   Easy to learn: Java is a easy to learn programming language. Once you start learning Java programming, you will actually be learning the real programming and you will observe everyth...

Java User Input Exercise

Exercise 1: Write a simple java program to take user input of a number and store it inside an integer variable. Later print the number on the screen.   Solution 1:   import java.util.Scanner;   public class UserInpur {        public static void main(String[] args ) {               Scanner sc = new Scanner(System. in );           System. out .println( "Enter a number" );           int x = sc .nextInt();           System. out .println( "You have entered:" );                   System. out .println( x );        }   }   Exercise 2: Write a simple java program to tak...

Java Operators

In this lesson you are goin g to learn about operators in Java and also what are the different types of operators used in Java programming.   In our previous lesson we have learnt how to take user input in Java. We were performing different types of arithmetic operations in order to create a simple ration shop billing application which took different values as the user input and calculated total billing amount.   During the programming the billing application we used different signs like addition (+) , multiplication (*) etc. These signs are called operators in Java programming.   Types of Operators in Java There are five types of operators in Java. Arithmetic Operators Bitwise Operators Assignment Operators Comparison Operators Logical Operators 1.Java Arithmetic Operators Arithmetic operators are used to perform mathematical operations such addition, division, multiplication, subtraction and modulation. Let’s see the example of arithmetic operators in below:   ...