Examples for taking User input
User input by command line argument example
User input by Scanner class example
User input by BufferedReader class example
Take Input From BufferReader
User input by command line argument example
class StaticDemo
{
public static void main(String s1[])
{
int a=Integer.parseInt(s1[0]);
float b=Float.parseFloat(s1[1]);
String name=String.valueOf(s1[2]);
System.out.println("addition of "+a+" and "+b+" = "+(a+b));
System.out.println("name = "+name);
}
}
Output
C:\Java>java StaticDemo 10 10.23 jtechies addition of 10 and 10.23 = 20.23 name = jtechies
User input by Scanner class example
User input by Scanner class example
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class Scanner_Demo
{
public static void main(String[] args) throws IOException
{
Scanner scanner =new Scanner(System.in);
System.out.println("Enter the name = ");
String name=scanner.nextLine();
System.out.println("Enter the first number = ");
int a=scanner.nextInt();
System.out.println("Enter the second number = ");
int b=scanner.nextInt();
System.out.println("name is = "+name);
System.out.println("addition of "+a+" and "+b+" = "+(a+b));
}
}
Output
Enter the name = jtechies Enter the first number = 20 Enter the second number = 50 name is = jtechies addition of 20 and 50 = 70
User input by BufferedReader class example
User input by BufferedReader class example
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class StaticDemo
{
public static void main(String s1[]) throws IOException
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter the name = ");
String name=br.readLine();
System.out.println("Enter the first number = ");
int a=Integer.parseInt(br.readLine());
System.out.println("Enter the second number = ");
int b=Integer.parseInt(br.readLine());
System.out.println("name is = "+name);
System.out.println("addition of "+a+" and "+b+" = "+(a+b));
}
}
Output
Enter the name = jtechies Enter the first number = 54 Enter the second number = 45 name is = jtechies addition of 54 and 45 = 99
Take Input From BufferReader
Take Input From BufferReader
package com.buffered;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Main
{
public static void main(String[] args)
{
PressAnyKey();
}
public static void PressAnyKey()
{
BufferedReader input =newBufferedReader
(newInputStreamReader(System.in));
System.out.print("Press any key...");
try
{
input.readLine();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
Output
Press any key...


