Java User Input

Java scanner class is provide few standard function to deal user inputs. scanner class are exist in java.util package so before use of this class we are need to import this package.

//import package for Scanner class
import java.util.Scanner;
public class Execution{

   
    public static void main(String[] args) {
        //make object of scanner class
        Scanner action=new Scanner(System.in);

        System.out.print("Enter your hobby : ");
        String hobby=action.nextLine();//get input from keyboards

        //display user input value
        System.out.println("\nYour hobby is :"+hobby);

    }
}

Output

Enter your hobby : Cricket 

Your hobby is :Cricket

nextLine() are fetch the user input value. and in this case assigned of this value to string variable.

Java User Input

Help of scanner class we can access all available standard method to user input mechanism. in this example, when execute this source code. nextLine() method are get input by keyboard. nextLine() method is used to input string value. there are following methods are also available to input other data type values.

Method Overview
nextByte() Use to get Byte value from user
nextBoolean() Use to get Boolean value from user
nextShort() Use to get Short value from user
nextInt() Use to get Integer value from user
nextLong() Use to get Long value from user
nextFloat() Use to get Float value from user
nextDouble() Use to get Double value from user
nextLine() Use to get string value from user

For example.

//import package for Scanner class
import java.util.Scanner;
public class Execution{

   
    public static void main(String[] args) {
        //Make object of scanner class
        Scanner action=new Scanner(System.in);

        System.out.print("Enter Product Name : ");
        String product=action.nextLine(); //get string value

        System.out.print("Enter product quantity : ");
        int quntity=action.nextInt();

        System.out.print("Enter Price of each item: ");
        double price=action.nextFloat();

        //Display input value
        System.out.println("Product Name : "+product);
        System.out.println("Quntity      : "+quntity);
        System.out.println("Price        : "+price);
        System.out.println("Total Amount : "+(price*quntity));
     

    }
}

Output

Enter Product Name : apple iphone 8    
Enter product quantity : 3
Enter Price of each item: 60000
Product Name : apple iphone 8
Quntity      : 3
Price        : 60000.0
Total Amount : 180000.0

In this example trying to convert valid input value to relative data type such a case when if is not possible to convert input value to other data type format the compiler are produced an exception. And that is terminate program abnormally. You'll can handling this program using exception handling.

Scanner Class Public methods

There is various method which are accessible in java.util.Scanner class.

Method Syntax
radix public int java.util.Scanner.radix()
ioException public java.io.IOException java.util.Scanner.ioException()
delimiter public java.util.regex.Pattern java.util.Scanner.delimiter()
useDelimiter public java.util.Scanner java.util.Scanner.useDelimiter(java.lang.String)
useDelimiter public java.util.Scanner java.util.Scanner.useDelimiter(java.util.regex.Pattern)
useLocale public java.util.Scanner java.util.Scanner.useLocale(java.util.Locale)
useRadix public java.util.Scanner java.util.Scanner.useRadix(int)
hasNextLine public boolean java.util.Scanner.hasNextLine()
findInLine public java.lang.String java.util.Scanner.findInLine(java.lang.String)
findInLine public java.lang.String java.util.Scanner.findInLine(java.util.regex.Pattern)
findWithinHorizon public java.lang.String java.util.Scanner.findWithinHorizon(java.util.regex.Pattern,int)
findWithinHorizon public java.lang.String java.util.Scanner.findWithinHorizon(java.lang.String,int)
hasNextBoolean public boolean java.util.Scanner.hasNextBoolean()
nextBoolean public boolean java.util.Scanner.nextBoolean()
hasNextByte public boolean java.util.Scanner.hasNextByte()
hasNextByte public boolean java.util.Scanner.hasNextByte(int)
nextByte public byte java.util.Scanner.nextByte(int)
nextByte public byte java.util.Scanner.nextByte()
hasNextShort public boolean java.util.Scanner.hasNextShort(int)
hasNextShort public boolean java.util.Scanner.hasNextShort()
nextShort public short java.util.Scanner.nextShort(int)
nextShort public short java.util.Scanner.nextShort()
hasNextInt public boolean java.util.Scanner.hasNextInt()
hasNextInt public boolean java.util.Scanner.hasNextInt(int)
hasNextLong public boolean java.util.Scanner.hasNextLong(int)
hasNextLong public boolean java.util.Scanner.hasNextLong()
nextLong public long java.util.Scanner.nextLong(int)
nextLong public long java.util.Scanner.nextLong()
hasNextFloat public boolean java.util.Scanner.hasNextFloat()
nextFloat public float java.util.Scanner.nextFloat()
hasNextDouble public boolean java.util.Scanner.hasNextDouble()
hasNextBigInteger public boolean java.util.Scanner.hasNextBigInteger(int)
hasNextBigInteger public boolean java.util.Scanner.hasNextBigInteger()
nextBigInteger public java.math.BigInteger java.util.Scanner.nextBigInteger(int)
nextBigInteger public java.math.BigInteger java.util.Scanner.nextBigInteger()
hasNextBigDecimal public boolean java.util.Scanner.hasNextBigDecimal()
nextBigDecimal public java.math.BigDecimal java.util.Scanner.nextBigDecimal()
nextLine public java.lang.String java.util.Scanner.nextLine()
remove public void java.util.Scanner.remove()
toString public java.lang.String java.util.Scanner.toString()
hasNext public boolean java.util.Scanner.hasNext(java.util.regex.Pattern)
hasNext public boolean java.util.Scanner.hasNext(java.lang.String)
hasNext public boolean java.util.Scanner.hasNext()
next public java.lang.String java.util.Scanner.next()
next public java.lang.Object java.util.Scanner.next()
next public java.lang.String java.util.Scanner.next(java.util.regex.Pattern)
next public java.lang.String java.util.Scanner.next(java.lang.String)
close public void java.util.Scanner.close()
skip public java.util.Scanner java.util.Scanner.skip(java.lang.String)
skip public java.util.Scanner java.util.Scanner.skip(java.util.regex.Pattern)
reset public java.util.Scanner java.util.Scanner.reset()
nextInt public int java.util.Scanner.nextInt(int)
nextInt public int java.util.Scanner.nextInt()
nextDouble public double java.util.Scanner.nextDouble()
match public java.util.regex.MatchResult java.util.Scanner.match()
locale public java.util.Locale java.util.Scanner.locale()
wait public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
wait public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
wait public final void java.lang.Object.wait() throws java.lang.InterruptedException
equals public boolean java.lang.Object.equals(java.lang.Object)
hashCode public native int java.lang.Object.hashCode()
getClass public final native java.lang.Class java.lang.Object.getClass()
notify public final native void java.lang.Object.notify()
notifyAll public final native void java.lang.Object.notifyAll()
forEachRemaining public default void java.util.Iterator.forEachRemaining(java.util.function.Consumer)


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved