+ 2
why does this code not work (Scanner use)
import java.util.Scanner; class MyClass { public static void main(String[ ] args) { Scanner store = new Scanner(System.in); System.out.printIn("What's your age?"); int age = store.nextLine(); System.out.printIn("Your age is: " + age); } }
10 Answers
+ 5
Correct code:
import java.util.Scanner;
class MyClass {
public static void main(String[] args) {
Scanner store = new Scanner(System.in);
System.out.println("What's your age?");
int age = store.nextInt();
System.out.println("Your age is: "+ age);
}
}
+ 4
https://code.sololearn.com/cJEOUok0bRuK/?ref=app
See this you will have your problem solved
+ 3
nextLine() is to take string from user
nextInt() is to take integer from user
In your code, you are taking string as input but data type was int that's why it wasn't working.
+ 2
@Moro If it helped, Mark it as best.
+ 2
My pleasure @Moro
+ 1
Thank you!!!
+ 1
I dont know how, and exactly what did you change apart store.nextInt();? I tried it myself and it didnt work
+ 1
Also why doesn't store.nextLine(); not work? It's supposed to read the line whatever it is, or am i wrog
+ 1
I tried the same thing but it still doesn't work
import java.util.Scanner;
class MyClass {
public static void main(String[ ] args) {
Scanner store = new Scanner(System.in);
System.out.printIn("What's your age?");
int age = store.nextInt();
System.out.printIn("Your age is: " + age);
}
}
+ 1
Ohh thank you!