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