+ 1
Input Java
How to use the value obtained in the scanner object as a variable? I mean the variable's value to be what is get in the scanner object.
3 Answers
+ 16
â Import Scanner class
//import java.util.Scanner;
â create object of Scanner class
//Scanner sc = new Scanner(System.in);
â make use of methods like .nextInt() , .next() , .nextDouble() to take inputs of different types [no need to create object multiple times , just use method of same object multiple times]
//example :
String str = sc.next();
Syring res = sc.next();
int a = sc.nextInt()
double b = sc.nextDouble();
you can see list of input methods for different types here :
https://www.sololearn.com/learn/Java/2220/
+ 4
Maybe java experts can help
Gaurav Agrawal
Muhd Khairul Amirin Bin Yaacob
My try :
https://code.sololearn.com/cLGD8FCXFYM8/?ref=app
+ 2
initialize an object for scanner first(but first, you've to import Scanner using import java.util.Scanner):
Scanner sc = new Scanner(System.in);
int x = sc.nextInt(); //assigns integer input to variable x
you can use nextDouble(); nextLong() etc for corresponding variables.