Well, in chit chat zabu said we have misued scanner for user input, it's intended to be used for file inputs. And IO, is the real deal for input now and for output too probably. I have used Console for Input too. But, what puzzles me that what makes someone decide what's good for what? Like IO good for user input and Scanner for file input. Is it the interfaces implemented under the hood or something else? Teach me some java today, please^^
#Scanner, IO, Console, etc
1 messages · Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.
Well, in chit chat zabu said we have misued scanner for user input, it's intended to be used for file inputs. And IO, is the real deal for input now and for output too probably. I have used Console for Input too. But, what puzzles me that what makes someon
Scanner class in Java is commonly used for user input, but it is actually intended for file inputs. For user input, IO classes or Console class should be used.
Useful links:
- AI generated response
its mostly about how the class is designed
and its methods
scanner is good for tokenizing, doesnt matter where the data stream is coming from
tokenizing means splitting a text based resource into several chunks based on a separator for example
for example all words in a sentence
scanner has several problematic design choices for the use case of user input:
why do we need an instance in the first place? we want user input, so no need for instances.
that also gets rid of the problem that when u create multiple scanners on the same resource u run into issues with buffering
then the problem with all the nextX and hasX methods that we simply dont need or want for user input
then scanner being intended for any resource, it implements AutoClosable. consequently VSC shows a warning if u forget to close a scanner - but thats wrong for scanners tied to System.in
the new class IO was specifically designed for the use case of user input via console. so it has none of these pitfalls
but therefore its of limited use, its not a general purpose powerful and capable class like Scanner
Just on your point on "what makes someone decide what's good" is subjective, usually something good is what makes the job easier and nicer to do.
An unrelated example might be "what web framework do I use? There's so many!" - to that the answer is the same, the one that's easy to work with solves all the problems 😋
Hmmm. I see. So there is no special ingredient i guess. Use what works well for you. With experience you get to know that Scanner has pitfalls for general input output, so use IO since it's easier to use. But IO isn't as good as Scanner when it comes to doing other stuff like zabu said, scanner does tokenisation and so it's useful for File inputs, as File may consist of many kinds of tokens like Int, Float, Char etc. Ohhh so maybe that's where hasNextX() or nextX() methods comes in handy. And for user input just go with IO.readln() or IO.println() with some parsing. Hmm okay sounds good.
👍