Friday, August 21, 2009

Java : Get System Input using Scanner class

Simple program to scan input from user using Scanner class. You can get Long, Shot, Byte, String, Int etc.


/* ScanInput.java */

import java.util.Scanner;

public class ScanInput
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);

System.out.println("Enter String : ");

String strVal = scan.nextLine();

System.out.println("You Entered : "+strVal);

System.out.println("Enter Int : ");

int intVal = scan.nextInt();

System.out.println("You Entered : "+intVal);

scan.close();

}
}


/******OUTPUT*********/


Enter String :
This is a string
You Entered : This is a string
Enter Int :
234
You Entered : 234