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?
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
+ 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);
+ 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();