how to use Scanner class using collections in java
Previous | Home | Next |
package r4r.co.in;
import java.util.*;
public class CollectionsExample {
/**
* @param args
*/
public static void main(String[] args) throws IllegalArgumentException
{
// TODO Auto-generated method stub
try
{
Scanner scanner=new Scanner(System.in); // here we have created an object of Scanner class which will take the input via keyboard
System.out.print("Enter first number: ");
int number1 = scanner.nextInt(); // we are taking first number as input from user
System.out.println("Enter second number: ");
int number2 = scanner.nextInt(); // we are taking second number as input from user
int sum ;
sum = number1+number2; // storing the sum of the two numbers in sum
System.out.println("The sum of the two numbers is: "+sum); // displaying the value of sum
}
catch(IllegalArgumentException e)
{
e.printStackTrace();
}
}
}
The output of the above example is:
Enter first number:
70
Enter second number:
30
The sum of the two numbers is: 100
Previous | Home | Next |