Please correct my code, thank you | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Please correct my code, thank you

please let me know where is the problem! import java.util.Scanner; class program{ public static void main(String args[]){ int age,money; System.out.println("enter your age"); Scanner a = new Scanner(System.in); age = a.nextInt(); System.out.println("enter money you have"); Scanner m = new Scanner(System.in); money = m.nextInt(); if( age>= 18 && money>500){ System.out.println("welcome"); } else {System.out.println("error");} } }

31st Jul 2018, 3:43 AM
kingvarma
kingvarma - avatar
3 Answers
+ 4
Yes, single scanner is enough for all data types. You'll just have to call different methods for different data types. (like nextInt() for int, nextDouble() for double etc.) Example code: https://code.sololearn.com/cWOQlJ1re4p8/#java Reference on multiple scanners: https://stackoverflow.com/questions/29053580/cannot-use-multiple-scanner-objects-in-java/29053677
31st Jul 2018, 4:53 AM
Shamima Yasmin
Shamima Yasmin - avatar
+ 3
Use one scanner for all inputs. Change the line of money's input to: money = a.nextInt(); Also remove the declaration of m. :)
31st Jul 2018, 4:09 AM
Shamima Yasmin
Shamima Yasmin - avatar
0
can't we call another scanner with defferent variable in the same sintax? is that a single scanner enough for all data types? thanks again for clarifying.
31st Jul 2018, 4:15 AM
kingvarma
kingvarma - avatar