How to Get Input from User in Java from Command Line : 3 Approach

Java Code

 

Are you looking for How to get input from user in java from command Line ?

Actually There are three easy and possible ways in java for taking input from user in command line . Lets go one by one .

How to get input from user in java using Bufferedreader Class-

1.1 – String ( input from user in java using Bufferedreader Class ) –

 

//BufferedReader object creation 
BufferedReader readerObj = new BufferedReader(new InputStreamReader(System.in));
 //readLine function for reading string 
String UserInputString = readerObj.readLine(); 
//Print the the String Input System.out.println(UserInputString);

1.2 – Integer  ( input from user in java using Bufferedreader Class)-

Now ervery thing will be same as above . I mean you have to take the input as String and then covert into required Format .Here is the code sample  for how to convert String to Integer in java .

// code for taking Input as String 
 BufferedReader readerObj = new BufferedReader(new InputStreamReader(System.in));
 String UserInputString = readerObj.readLine(); 
// code to convert string into java  
int userInputInteger = Integer.parseInt(userInputString);

Note – You may get run time exception if the input String can not be converted into Integer.For example is user input the string like “23as”  then it is not a valid Integer while parsing . In that situation you may get exception link this –

how to get input from user in java Exception in BufferedReader
how to get input from user in java Exception in BufferedReader

1.3-   Float /Double  (input from user in java using Bufferedreader Class)-

In the same way as we did the parsing of String into Integer , We can do it with Float and Double . Here is the Syntax for conversion of String into Float and Double .

//code for taking Input as String 
 BufferedReader readerObj = new BufferedReader(new InputStreamReader(System.in)); 
String UserInputString = readerObj.readLine(); 
//Coverting the String into Double 
double userInputDouble = Double.parseDouble(userInputString);

Complete code for BufferedReader Class Input –

Here is the complete code for – How to get input from user in java from command Line using Buffered Reader Class.

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class InputExampleUsingBufferedReader {
public static void main (String args[]) throws IOException
{
//BufferedReader object creation
BufferedReader readerObj = new BufferedReader(new InputStreamReader(System.in));
//readLine function for reading string
// Reading data using readLine
String userInputString = readerObj.readLine();
//Print the the String Input
System.out.println(userInputString);
//Coverting the String into Integer
int userInputInteger = Integer.parseInt(userInputString);
System.out.println(" /n Here is coverted Integer from the Input String" +userInputInteger);
//Coverting the String into Double
double userInputDouble = Double.parseDouble(userInputString);
System.out.println(" /n Here is coverted Double from the Input String" +userInputDouble);
//Coverting the String into Float
float userInputFloat = Float.parseFloat(userInputString);
System.out.println(" /n Here is coverted Float from the Input String" +userInputFloat);
}
}

 

 How to get input from user in java using Scanner Class –

Here first you need to create Scanner Class object . Once you have the object you may call functions likenextInt() for integer Input and nextLong()for long input etc . Below is the complete example for that .

 

import java.util.Scanner;
public class InputExampleUsingScanner {
public static void main(String args[]) {
//Scanner Object creation
Scanner scannerObj= new Scanner(System.in);
//Integer Input using Scanner 
int userInputInteger=scannerObj.nextInt();
//String Input using Scanner 
String userInputString =scannerObj.nextLine();
/Double Input using Scanner 
Long userInputLong=scannerObj.nextLong();
}
}

How to get input from user in java using Console Class –

In this section before jumping into code part , It is essential to introduce System.class in java . This class is available in (Java.lang) package . You need not to initiate this class .

Now lets jump into code part –

 

//console() function for reading string 
String userInputString = System.console().readLine();

 

hink it will be more clear to see the full example . Specially I am focusing more on the part where I have said , You need not to initiate System.class explicitly .

 

public class UserInputUsingConsoleFunction 
{
 public static void main(String[] args) 
{ 
// How to invoke System.console() method 
String userInputString = System.console().readLine(); 
System.out.println(userInputString);
 } 
}

 

Note – You can not run directly on IDE . You need console to run this code . If you run it on IDE , You will get Exception .

Which One Java user Input method is better –

  1. BufferedReader and console.readLine() are synchronized . Although Scanner is not synchronized .
  2. Syntax wise BufferedReader is little difficult than other two .
  3. You can avoid displaying user input screen with console.readLine()  . So it is more prefer where password or any other confidential information is needed .

Bonus Tips –

Although this article is beneficial in every field of software development with java  because its Core Java Understanding . In case you are more Interested in Data Science with Java , Must read the article –  How a Java Engineer can Transform his career into Data Science | Java for Data Science ?
Thanks

Data Science Learner  Team 

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Abhishek ( Chief Editor) , a data scientist with major expertise in NLP and Text Analytics. He has worked on various projects involving text data and have been able to achieve great results. He is currently manages Datasciencelearner.com, where he and his team share knowledge and help others learn more about data science.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner