0

How do you get an input from the user and store it in a variable in java?

Ive tried to scan a number and store it in a variable in Java.But it seems to be easy in c or c++ because the scanf or cin func. is a bit comfortable.How does the scanner function in Java work?

13th May 2018, 5:02 PM
ValarTyrin
ValarTyrin - avatar
3 Respuestas
+ 6
import java.util.scanner; class abc{ Scanner sc=new Scanner(System.in); System.out.println("Enter a no. "); int x=sc.nextInt(); System.out.println("Enter any variable"); int y = sc.next(); } https://www.sololearn.com/learn/Java/2220/?ref=app
13th May 2018, 5:08 PM
Karan Kumar
Karan Kumar - avatar
+ 13
import java.util*; //main Scanner scn = new Scanner(System.in); String str = scn.nextLine(); //or int n = scn.nextInt(); System.out.println(Your Variable);
13th May 2018, 5:07 PM
D_Stark
D_Stark - avatar
+ 4
You need to import the scanner class. Create a scanner instance like this: Scanner s = new Scanner(input); Then you can simply put it in a variable like: X = s.nextInt();
13th May 2018, 5:08 PM
Lord Potato
Lord Potato - avatar