<div dir="ltr">> 
Scanner has a ton of complexity in it that can easily trip up beginners. <br><div><br></div><div>This part should be said again and louder.</div><div><br></div><div>As someone who spends a lot of time assisting new programmers online (mostly Discord/Reddit), this is a multiple-times-a-day, every day of the week problem (and this is not hyperbole).</div><div><br></div><div>This occurs so very often that many such communities have prepared bot-responses making exactly Brian's point (using `==` with `String` is probably the other really common one).<br></div><div><br></div><div>
<div>`Scanner` is a highly versatile tool for blasting holes in the feet of beginners. And promoting `Scanner.nextInt()` when you really mean "line of input that is an int" is aiming the gun.<br></div></div><div><br></div><div>The individual concepts have to be visible or we'll continue to maim new programmers.<br></div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Feb 19, 2024 at 10:44 AM Brian Goetz <<a href="mailto:brian.goetz@oracle.com">brian.goetz@oracle.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">I think you are counting characters and not counting concepts.<br>
<br>
Scanner has a ton of complexity in it that can easily trip up <br>
beginners.  The main sin (though there are others) is that input and <br>
parsing are complected (e.g., nextInt), which only causes more problems <br>
(e.g., end of line issues.)   Reading from the console is clearly a () <br>
-> String operation.  The input() method does one thing, which is get a <br>
line of text.  That's simple.<br>
<br>
Integer.parseInt (or, soon, patterns that match against string and bind <br>
an int) also does one thing: convert a string from int.  It may seem <br>
verbose to have to do both explicitly, but it allows each of these <br>
operations to be simple, and it is perfectly obvious what is going on.  <br>
On the other hand, Scanner is a world of complexity on its own.<br>
<br>
Console::readLine is nice, but first you have to get a Console. ("Why <br>
can I print something without having to get some magic helper object, <br>
but I can't do the same for reading?")  What we're optimizing for here <br>
is conceptual simplicity; the simplest possible input method is the <br>
inverse of println.  The fact that input has to be validated is a fact <br>
of life; we can treat validation separately from IO (and we should), and <br>
it gets simpler when you do.<br>
<br>
On 2/18/2024 4:12 PM, Cay Horstmann wrote:<br>
> I would like to comment on the simplicity of <br>
> <a href="https://openjdk.org/jeps/8323335" rel="noreferrer" target="_blank">https://openjdk.org/jeps/8323335</a> for beginning students.<br>
><br>
> I am the author of college texts for introductory programming. Like <br>
> other authors, I introduce the Scanner class (and not Console) for <br>
> reading user input. Given that students already know about System.out, <br>
> it is simpler to call<br>
><br>
> System.out.print("How old are you? ");<br>
> int x = in.nextInt(); // in is a Scanner<br>
><br>
> than<br>
><br>
> int x = Integer.parseInt(console.readLine("How old are you? "));<br>
><br>
> or with the JEP draft:<br>
><br>
> int x = Integer.parseInt(input("How old are you? "));<br>
><br>
> Then again, having a prompt string is nice too, so I could imagine <br>
> using the Console API with Integer.parseInt and Double.parseDouble, <br>
> instead of Scanner.nextInt/nextDouble.<br>
><br>
> But why have a third API, i.e. "input"?<br>
><br>
> I think there are two feasible directions. Either embrace the Scanner <br>
> API and next/nextInt/nextDouble/nextLine, or the Console API and <br>
> readLine. Adding "input" into the mix is just clutter, and ambiguous <br>
> clutter at that. At least readLine makes it clear that the entire line <br>
> is consumed.<br>
><br>
> Cheers,<br>
><br>
> Cay<br>
><br>
> -- <br>
><br>
> Cay S. Horstmann | <a href="http://horstmann.com" rel="noreferrer" target="_blank">http://horstmann.com</a> | mailto:<a href="mailto:cay@horstmann.com" target="_blank">cay@horstmann.com</a><br>
<br>
</blockquote></div>