[Java] What class or method might be used for asking the user to enter a value? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

[Java] What class or method might be used for asking the user to enter a value?

The Scanner class can ask for a parameter but, what if I want a personalized message asking for a particular kind of data?

8th Feb 2017, 1:05 AM
Martín Lehoczky
Martín Lehoczky - avatar
2 Answers
+ 5
You can use one Scanner for all the differents types. Scanner input = new Scanner(System.in); input.nextLine(); input.nextInt(); input.nextDouble(); input.next().charAt(0); input.nextBoolean()
8th Feb 2017, 6:22 AM
Vânia Almeida
Vânia Almeida - avatar
+ 2
you assign your type as you see fit and call the scanner to look for it.. Scanner strIn = new Scanner(System.in); String name=strIn.nextLine(); Scanner intIn = new Scanner(System.in); int number=input.nextInt(); I did read it is bad to use a scanner for one type after using it for another type so I generally make a scanner for each type be it a string or an integer
8th Feb 2017, 1:09 AM
LordHill
LordHill - avatar