+ 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); } }

3rd Sep 2017, 3:04 AM
MoroAstray
MoroAstray - avatar
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); } }
3rd Sep 2017, 3:11 AM
Kartikey Sahu
Kartikey Sahu - avatar
+ 4
https://code.sololearn.com/cJEOUok0bRuK/?ref=app See this you will have your problem solved
3rd Sep 2017, 3:32 AM
Kartikey Sahu
Kartikey Sahu - avatar
+ 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.
3rd Sep 2017, 3:29 AM
Kartikey Sahu
Kartikey Sahu - avatar
+ 2
@Moro If it helped, Mark it as best.
3rd Sep 2017, 3:17 AM
Kartikey Sahu
Kartikey Sahu - avatar
+ 2
My pleasure @Moro
3rd Sep 2017, 3:33 AM
Kartikey Sahu
Kartikey Sahu - avatar
+ 1
Thank you!!!
3rd Sep 2017, 3:16 AM
MoroAstray
MoroAstray - avatar
+ 1
I dont know how, and exactly what did you change apart store.nextInt();? I tried it myself and it didnt work
3rd Sep 2017, 3:21 AM
MoroAstray
MoroAstray - avatar
+ 1
Also why doesn't store.nextLine(); not work? It's supposed to read the line whatever it is, or am i wrog
3rd Sep 2017, 3:23 AM
MoroAstray
MoroAstray - avatar
+ 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); } }
3rd Sep 2017, 3:31 AM
MoroAstray
MoroAstray - avatar
+ 1
Ohh thank you!
3rd Sep 2017, 3:33 AM
MoroAstray
MoroAstray - avatar