Java Inputs
You can already show the data to the users.
Now let's receive it from user.
In java reading the input is a bit complicated
We have a Scanner class in java
the Scanner class have methods to read different type of input.
There are some of it that we use frequently
nextLine
read the whole input line until found a line separator delimiter.next
read the input until found a blank space (Space bar key)nextInt
read the input as an IntegernextDouble
read the input as a floating point number
read more here
You have to use the Scanner class as show here
import java.util.Scanner;
public class LearnInput {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter your name: ");
String name = sc.nextLine();
System.out.println("Enter your age: ");
int age = sc.nextInt();
System.out.println("Enter your salary: ");
double salary = sc.nextDouble();
System.out.println("Your name is " + name);
System.out.println("Your age is " + age);
System.out.println("Your salary is " + salary);
sc.close();
}
}
first import Scanner class
Then declare a new Scanner object (Here is named sc
)
Make sure to use the right data type to the right scanner method
No Comments