Reading user input in Java through stdin
To use class, an import keyword is used with java.util.Scanner:
The next step is to create a Scanner object “in”:
Here we are also creating a public class and its syntax is as follows.
……
}
In the main function we are required to make the program ready for taking input from the user. The next step is to declare variables for taking input from the user:
int y;
The following line asks the user to input two numbers and accepts their values using the in Scanner object. If you want to display something on the screen, then you can do that by using function System.out.println():
Whereas if you want to take input from the user you can do that by typing:
In the above line, the in.nextInt() reads the integer value from the keyboard that the user enters that will be stored in the variable x. So if you want to input two different integers from the user and want to calculate their sum then you can do that by following the below-mentioned code:
Now the complete code to calculate the sum of two numbers are shown below:
//Creating the main class
public class StdInput {
public static void main(String[] args) {
//Taking numbers as stdin and adding them
Scanner in=new Scanner(System.in);
System.out.println("Standard Input Example 1\n-----------------");
int x;
int y;
System.out.println("Please enter a number: ");
x=in.nextInt();
System.out.println("Enter another number: ");
y=in.nextInt();
int sum=x+y;
System.out.println("Sum of two numbers is: "+sum);
}
}
Note: To run and compile Java code in a Linux terminal you need JDK installed.
You can test the code by creating a java file using any text editor in Linux OS for example:
After that you can compile this file by typing:
After the compiling has been completed, you can run the code by typing:
Similarly in the following example, we are going to input the name as well as the temperature from the user in Fahrenheit. The string data type will be used to make a name as an input from the user and you can do that by typing:
On the other hand, we have used double data type for the temperature as its value can be infractions:
System.out.println("Please enter your name: ");
String name=in.next();
System.out.println("Hi "+ name);
System.out.println("Please enter temperature in fahrenheit: ");
double temp=in.nextDouble();
double celsius =(temp-32)*0.55556; //(temp-32)*5/9
System.out.println("Temperature in Celsius is: "+celsius);
So the full code for this example is:
//Creating the main class
public class MProgram {
public static void main(String[] args) {
//Taking user name and temperature as stdin
Scanner in=new Scanner(System.in);
System.out.println("\nStandard Input Example 2\n-----------------");
System.out.println("Please enter your name: ");
String name=in.next();
System.out.println("Hi "+ name);
System.out.println("Please enter temperature in fahrenheit: ");
double temp=in.nextDouble();
double celsius =(temp-32)*0.55556; //(temp-32)*5/9
System.out.println("Temperature in Celsius is: "+celsius);
}
}
Conclusion
Stdin is used to take the input from the user which is also known as standard input. In this article we have taught you how you can get the standard input from the user and for this, we have executed two different examples. In the first one we have taken two numbers from the user and then calculated their sum while in the second one, we have taken name and temperature as an input from the user, performed the operation of conversion (Fahrenheit to Celsius), and displayed this information on the screen.