3.GP-Java-Taking Input
3.GP-Java-Taking Input
In Java, there are mainly two ways to get the input from the user.
user. It is present in the java.util package. Scanner class is one of the most preferable
ways to take input from the user. This class is used to read the input of primitive types
such as int, double, long, etc. and String. You need to import the java.util package
Java Scanner class provides various methods to read different primitive data types from
the user.
Method Description
1
Example 1: Taking int value from the user
import java.util.Scanner;
class TakingInputFromUser {
public static void main(String argo[]) {
Output:
import.java.util.Scanner;
class TakingInputFromUser {
public static void main(String arg[]) {
Scanner sc = new Scanner(System.in);
System.out.println(“Enter a String : “);
// Read a string from the user
String str = sc.nextLine();
2
System.out.println(“Your entered string is : “ + str);
}
}
Output: