Pages

Read integer user input in java

Complete source code below, will show you how to wait and read integer user input in command prompt.

**********************************************************************
COMPLETE SOURCE CODE FOR : ReadIntegerUserInput.java
**********************************************************************


import java.util.Scanner;

public class ReadIntegerUserInput
{
public static void main(String[]args)
{
try
{
//Create a scanner object
//Scanner is a predefined class in java that will be use to scan text
//System.in is mean, we will receive input from standard input stream
Scanner readUserInput=new Scanner(System.in);

//Print into command prompt(What is your age ?)
System.out.println("What is your age ?");

//Wait and READ INTEGER INPUT from user after user hit 'Enter'
int myAge=readUserInput.nextInt();

//Print what store in myAge
System.out.println("Your age is : "+myAge);
}
catch(Exception exception)
{
//Error that will be print if user put other than number include
//point number
System.out.println("Hey !!! Don't put other than number");
}
}
}


**********************************************************************
JUST COMPILE AND EXECUTE IT
**********************************************************************
Related Posts Plugin for WordPress, Blogger...