To input the data, Java uses System.in. This is referring to the standard input device, and by default, the input device is a keyboard.
Console input is not directly supported in Java, but you can use the Scanner class to create an object to read from System.in as follows:
Scanner input = new Scanner(System.in);
An object may contain methods. Invoking a method on an object is to ask the object to perform a task. A Scanner object contains the methods for reading input, as the following:
next( ) read a word
nextLine( ) read a String (the rest of the current line)
nextInt( ) read an integer
nextFloat( ) read a float
nextDouble( ) read a double
nextLong( ) read a long
nextByte( ) read a byte
Example:
import java.util.Scanner;
class GetInputFromUser{
public static void main(String args[]){
int a;
float b;
String s;
Scanner in = new Scanner(System.in);
System.out.println("Enter a string");
s = in.nextLine();
System.out.println("You entered string "+s);
System.out.println("Enter an integer");
a = in.nextInt();
System.out.println("You entered integer "+a);
System.out.println("Enter a float");
b = in.nextFloat();
System.out.println("You entered float "+b);
}
}
We can't use the Scanner class directly to get a single letter to store in a char variable. So we use the next( ) method to get the next string that the user inputs. There's a next integer, next long, next double - even a next Boolean. But there's no next char. Even if the user inputs a single character it will still be a string and not a char.
We can use charAt method to get a character from any string that the user inputs, even if the user inputs a single letter:
char aChar = aString.charAt( 0);
All we're saying is "Get the character at position 0 in the string called aString, then store it in the aChar variable".
Example:
import java.util.Scanner;
public class MyFirstJavaProgram
{
public static void main(String []args) {
Scanner sc = new Scanner(System.in);
System.out.println("Quit Y/N");
char aChar = sc.next().charAt(0);
if (aChar == 'Y')
System.out.println("OK, BYE");
else
System.out.println("Not Quitting");
}
}
Modify the AreaCircle1 program so that user can input any value for the radius.
In our AreaCircle1 program, the value for radius is fixed. If we want to change the value, we need to change in the code.
public class AreaCircle1 {
public static void main(String[] args) {
double radius = 3;
double area = radius * radius * 3.14;
System.out.println("Area = " + area);
}
}
But, if we want to allow the user to input the value for radius during execution, we can use input statement like this;
radius = in.nextDouble();
Thus, our program will looks like:
public class AreaCircle2 {
public static void main(String[] args) {
double radius = in.nextDouble(); // read the radius
double area = radius * radius * 3.14;
System.out.println("Area = " + area);
}
}
To make it readable, add another statement:
public class AreaCircle2 {
public static void main(String[] args) {
System.out.println("Enter the radius: ");
double radius = in.nextDouble(); // read the radius
double area = radius * radius * 3.14;
System.out.println("Area = " + area);
}
}
Java uses System.out to refer to the standard output device. By default, the output device is the console. To perform the console output, we use println method to display the output to the console.
Therefore, statement:
System.out.println(area);
will print the value of the area to the console.
so, the output for the above program if the input is 3.0 is:
28.26
but if we modify the output statement to make it meaningful, like this:
System.out.println("Area = " + area);
We will get the output like this:
Area = 28.26
Or, modify to be like this:
System.out.println("Area for circle with radius " + radius + " is " + area);
and the output will be like this:
Area for circle with radius 20 is 28.26
So, the complete program is:
public class AreaCircle2 {
public static void main(String[] args) {
System.out.println("Enter the radius: ");
double radius = in.nextDouble();
double area = radius * radius * 3.14;
System.out.println ("Area for circle with radius " + radius + " is " + area);
}
}