right Syntax along with an example to create a scanner object
Answers
Answered by
3
Answer:
Explanation:
To import a class or a package, add one of the following lines to the very beginning of your code. After importing, we need to write the following statement in our program. Scanner s = new Scanner (System.in); Here by writing Scanner s, we are declaring s as an object of Scanner class.
Answered by
1
Answer:
Explanation:
The Scanner class is used to get user input, and it is found in the java.util package.
Scanner myObj, we are declaring myObj as an object of Scanner class. System.in within the round brackets tells Java that this will be System Input
import java.util.Scanner; // Import the Scanner class
import java.util.Scanner;
class MyClass {
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in);
System.out.println("Enter name, age");
String name = myObj.nextLine(); //for string use nextLine()
int age = myObj.nextInt(); //for int use nextInt()
// Output input by user
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}
Similar questions