Skip to content

Instantly share code, notes, and snippets.

@Blasanka
Last active April 9, 2017 18:13
Show Gist options
  • Save Blasanka/c76efdc09d203b9dc4890679b92bdfbf to your computer and use it in GitHub Desktop.
Save Blasanka/c76efdc09d203b9dc4890679b92bdfbf to your computer and use it in GitHub Desktop.
Here is how to get user input to java program using BufferedReader implementation and descripion as a comment
package SLCoder;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Buffer {
public static void main(String[] args)throws Exception {
//System.in get default input device - keyboard
//InputStreamReader create characters from collecting bytes
//then BufferedReader collect characters and make text line
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter your name here: ");
//readLine() is the method in BufferedReader class and it will create String text
// and here string add to the name variable
//reader is our object
String name = reader.readLine();
System.out.print("How old are you? ");
//I converted the input into integer because BufferedReader only get input as
//String. So if we want another data types we have to conver into that we want type
//Integer.parseInt() is a wrapper class for int
int age = Integer.parseInt(reader.readLine());
//here I check user input age is greater than the 18 then I will print the message
if(age > 18){
System.out.println(name + " You are adult person");
}else{
System.out.println("You are a little boy!");
}
//Using BufferedStream you can get any type of input and do manipulation
//If you want to get another data type like double
System.out.println("Enter your salary: ");
// like that you can use any data type
//but keep remember you have to change it to your variable type
//because of readLine() only take String
double salary = Double.parseDouble(reader.readLine());
System.out.println(salary);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment