Here is a java program to get input from user using Scanner class and Input Stream Reader + Buffered Reader class.
Using Scanner Class
Source Code:
//java program to take input from user
import java.util.Scanner;
class GetInput{
void getInputByUsr(){
Scanner sc = new Scanner(System.in);
try{
System.out.println("Enter your name:");
String name = sc.next();
System.out.println("Hello"+" "+name);
}
finally{
sc.close();
}
}
}
public class InputFromUser{
public static void main(String s[]){
GetInput ob = new GetInput();
ob.getInputByUsr();
}
}
Input & Output:
Enter your name:
techalmirah
Hello techalmirah
Using Input Stream Reader + Buffered Reader
Source Code:
package test;
// java program to get input from user using Input Stream Reader and Buffer Reader class
import java.io.*;
class GetInputs{
void getInput() throws IOException{
InputStreamReader ir = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(ir);
System.out.println("Enter name:");
String name = br.readLine();
System.out.println("welcome to"+" "+name);
}
}
public class InputUsingReader{
public static void main(String s[]) throws IOException{
GetInputs ob = new GetInputs();
ob.getInput();
}
}
Input & Output:
Enter name:
techalmirah
welcome to techalmirah