Website For Developers

Input in Java



Scanner Class

import java.util.Scanner; //import this package in your program


Syntax

Scanner objectName = new Scanner(System.inputStream);


Example of input in Java

import java.util.Scanner;
public class InputDemo {

   public static void main(String[] args) {
     Scanner scan = new Scanner(System.in);
     int age;
     String name,fname;

     System.out.println( "Enter your name" );
     name = scan.nextLine(); // For String

     System.out.println( "Enter your father name" );
     fname = scan.nextLine(); // For String

     System.out.println( "Enter your age" );
     age = scan.nextInt(); // For Integer

     System.out.println( "\nYou name is: "+name+"\nYour father name is: "+fname );
     System.out.println( "Your Age is:"+age );
   }
}

Output

Enter your name
Tom
Enter your father name
Moz
Enter your age
10

You name is: Tom
Your father name is: Moz
Your Age is:10