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?

22nd Apr 2022, 9:17 AM
jhh
7 Answers
+ 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...
22nd Apr 2022, 9:44 AM
Jayakrishna 🇮🇳
+ 2
Depending on type use want..! 1 to 3 are integers so use nextInt()...
22nd Apr 2022, 9:19 AM
Jayakrishna 🇮🇳
+ 1
it's your choice, nothing difference much you can use next().charAt(0); for single char Depends on the problem context..
22nd Apr 2022, 9:22 AM
Jayakrishna 🇮🇳
+ 1
So char or int is better than string in terms of single letter or char selection?
22nd Apr 2022, 9:24 AM
jhh
0
Even though is not meant for mathematic kind of data? Also use int?
22nd Apr 2022, 9:20 AM
jhh
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
23rd Apr 2022, 3:34 AM
jhh
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..
23rd Apr 2022, 10:40 AM
Jayakrishna 🇮🇳