Input Java | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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.

5th Jan 2019, 1:59 PM
João Batista dos Santos Sousa
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/
5th Jan 2019, 2:37 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
5th Jan 2019, 2:35 PM
Raj Chhatrala
Raj Chhatrala - avatar
+ 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.
5th Jan 2019, 2:36 PM
Asirap