0
Java scanner input
As for scanner type, if i only request the user to enter single digits for their choices of selection (for example option 1 - 3 ), which one would be more approriate like int type or string?
7 Respuestas
+ 3
There is no direct method to accept single character or single integer in java by scanner. next() accept a word but take charAt(0) for single character.. So which is better is not predictable.. because no difference. So use only depending on the task..
hope it helps...
+ 2
Depending on type use want..!
1 to 3 are integers so use nextInt()...
+ 1
it's your choice, nothing difference much
you can use next().charAt(0); for single char
Depends on the problem context..
+ 1
So char or int is better than string in terms of single letter or char selection?
0
Even though is not meant for mathematic kind of data? Also use int?
0
If i want to let user to key in 1 -3 , but allow them to key in other chararcter(which result in a loop that i wanted to )
For this case, i must declare variable for option to be a string? Because i use int will run time error
0
I understood clearly your query but are about storing input?
for single char by next().charAt(0) you can just use char type
char ch = in.next().charAt(0);
For int, take int type..
if not, try to specify code snippets will giving errors..