get user input

How to get User Input in Java?

In this chapter you will learn:get user input

  1. How to get Integer Input?
  2. How to get String Input?
  3. How to get Float Input?

This chapter demonstrates how to get user input in java. Various times while programming in java you need to accept some value from the user end. This value might be integer, string or float. As a java programmer you must know the input mechanism of java. Once you will finish this chapter, I am sure you will be master in accepting input in Java.

How to get user input in java?

To get input in java there are several classes which allow you to accept user end data but the most popular way is using Scanner Class.

Scanner Class

It is the most popular way to accept user input. Scanner class uses following method to accept different types of data. You must import java.util.Scanner class before using scanner method.

 

 Scanner sc=new Scanner(System.in);
 

 

Method Description
sc.next() Accept only one string and break when white space detected.
sc.nextLine() Accept full line of string with white space.
sc.nextByte() Accept byte data and store in byte variable.
sc.nextShort() Accept short data and store in short variable.
sc.nextInt() Accept number and store in integer variable.
sc.nextLong() Accept long value and store it in long variable.
sc.nextFloat() Accept float value and store it in float variable.
sc.nextDouble() Accept double value and store it in double variable.

Here we are writing a program which accept some user input and print the output. The program will ask following details from the user.

Name:
Age:
Mobile No:
Sex:
Qualification:

Programming Example

//You must write this line here for using Scanner class in program
import java.util.Scanner;

class ScannerClass
{
  public static void main(String[] args)
  {
    String name;
    int pin;
    Short age;
    Long mobileNumber;
   
    
    //Initializing Scanner Class
    Scanner sc=new Scanner(System.in);
    
    //Getting User Details
    System.out.println("Enter Your Name: ");
    name=sc.nextLine();   
    System.out.println("Enter your 4 Digit Pin Number: ");
    pin=sc.nextInt();
    System.out.println("Enter your age: ");
    age=sc.nextShort();    
    System.out.println("Enter your Mobile Number: ");
    mobileNumber=sc.nextLong();
    
    //Printing User Details
    System.out.println("********************User Details**********************");
    System.out.println("Name : " + name);
    System.out.println("Pin : " + pin);
    System.out.println("Age : " + age);
    System.out.println("MobileNumber : " + mobileNumber);
  
  }
}

Output

D:\JavaProgram>javac ScannerClass.java

D:\JavaProgram>java ScannerClass
Enter Your Name:
Christian Bale
Enter your 4 Digit Pin Number:
8851
Enter your age:
40
Enter your Mobile Number:
878765642

********************User Details**********************
Name : Christian Bale
Pin : 8841
Age : 40
MobileNumber : 878765642

D:\JavaProgram> __

Explanation

In the above example we added Scanner Library to program so that we can use Scanner class.

 //You must write this line here for using Scanner class in program
 import java.util.Scanner;


After that we have used several Scanner Object methods for accepting different types of value as

  name=sc.nextLine(); //For accepting string
  pin=sc.nextInt(); //For accepting integer
  age=sc.nextShort(); //For accepting short
  mobileNumber=sc.nextLong(); //For accepting Long


Summary

In this chapter you have learned how to accept user input in java. In the next chapter you will learn to define classes, objects and methods in java.

Leave a Reply

Your email address will not be published. Required fields are marked *