<div dir="ltr"><div dir="ltr">So with all that context, do you see why accepting <font face="monospace">Double.parseDouble(readln());</font><font face="arial, sans-serif"> might be worth it?<br><br>"Paving the onramp" shouldn't mean only "paving the first 48 hours." The convenience afforded by a dedicated </font><font face="monospace">readDouble</font><font face="arial, sans-serif">, at least to me, feels outweighed</font></div><div><font face="arial, sans-serif">by<br><br>* The loss of a perfectly good opportunity to explain the fundamentals of parsing (strings represent text, you can interpret that text by....)<br>* The loss of a perfectly good opportunity to teach basic exception handling<br>* The divergence with the behavior of Scanner<br>* The combinatorial explosion of "why not </font><font face="monospace">readByte</font><font face="arial, sans-serif">?"</font></div><div><font face="arial, sans-serif">* The privileged position it puts primitives in as the end-point of interpreting user input<br>* Guns, Feet<br><br>(I've separately voiced my concerns about </font><font face="monospace">readln</font><font face="arial, sans-serif"> in this regard.)</font></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Nov 7, 2024 at 12:40 PM Kenneth Fogel <<a href="mailto:kfogel@dawsoncollege.qc.ca">kfogel@dawsoncollege.qc.ca</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"><div class="msg-4664846296377142745">
<div lang="EN-CA" style="overflow-wrap: break-word;">
<div class="m_-4664846296377142745WordSection1">
<p class="MsoNormal"><span style="font-size:11pt">Here is an example of a Beethoven test passing routine that expects a number for 1 to 10. The Scanner, sc, has already been initialized. I also taught how to use regular expressions
to fine tune what is acceptable input and the acceptable range such that you only need to use readLine so that subsequent input is not messed up when a user enters 23 45 instead of 23.45. Notice that this routine cleans out the buffer with a nextLine at the
end. <u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt"> int number;<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt"> do {<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt"> System.out.println("Enter a number between 1 and 10: ");<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt"> if (sc.hasNextInt()) { // Check that there is an integer in the keyboard buffer<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt"> number = sc.nextInt(); //
<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt"> // Check if the number is in range<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt"> if (number < 1 || number > 10) {<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt"> System.out.println("Number out of range.");<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt"> }<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt"> } else { // There was not an integer in the keyboard buffer<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt"> number = -1; // a value that will keep execution in the loop<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt"> System.out.println("You have not entered a number");<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt"> }<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt"> sc.nextLine(); // Clean out the buffer<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt"> } while (number < 1 || number > 10);<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt">This tells me that in the paving the onramp universe a readInt or readDbl must clear the keyboard buffer when it encounters a terminating character such as the space, tab, and \n.
Already I can hear the roar over how this breaks the expected behaviour of Scanner such as allowing a list of primitives in the keyboard buffer.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt">Ken<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt"><u></u> <u></u></span></p>
<div style="border-right:none;border-bottom:none;border-left:none;border-top:1pt solid rgb(225,225,225);padding:3pt 0cm 0cm">
<p class="MsoNormal"><b><span lang="EN-US" style="font-size:11pt;font-family:Calibri,sans-serif">From:</span></b><span lang="EN-US" style="font-size:11pt;font-family:Calibri,sans-serif"> Ethan McCue <<a href="mailto:ethan@mccue.dev" target="_blank">ethan@mccue.dev</a>>
<br>
<b>Sent:</b> November 7, 2024 11:43 AM<br>
<b>To:</b> Kenneth Fogel <<a href="mailto:kfogel@dawsoncollege.qc.ca" target="_blank">kfogel@dawsoncollege.qc.ca</a>><br>
<b>Cc:</b> <a href="mailto:discuss@openjdk.org" target="_blank">discuss@openjdk.org</a><br>
<b>Subject:</b> Re: How about a readln for numbers?<u></u><u></u></span></p>
</div>
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<div>
<p class="MsoNormal">Currently, "don't mix nextLine with next/next int/etc" is an extremely common footgun.<br>
<br>
In one of the coding help discords, this is the auto message we send when people run into trouble with that.<br>
<br>
<span style="font-size:10.5pt;font-family:"Noto Sans",sans-serif;border:1pt none windowtext;padding:0cm">Mixing any
</span><code><span style="font-size:9pt;border:1pt none windowtext;padding:0cm">nextXXX</span></code><span style="font-size:10.5pt;font-family:"Noto Sans",sans-serif;border:1pt none windowtext;padding:0cm"> method with
</span><code><span style="font-size:9pt;border:1pt none windowtext;padding:0cm">nextLine</span></code><span style="font-size:10.5pt;font-family:"Noto Sans",sans-serif;border:1pt none windowtext;padding:0cm"> from the
</span><code><span style="font-size:9pt;border:1pt none windowtext;padding:0cm">Scanner</span></code><span style="font-size:10.5pt;font-family:"Noto Sans",sans-serif;border:1pt none windowtext;padding:0cm"> class for user input, will not ask you for input
again but instead result in an empty line read by </span><code><span style="font-size:9pt;border:1pt none windowtext;padding:0cm">nextLine</span></code><span style="font-size:10.5pt;font-family:"Noto Sans",sans-serif;border:1pt none windowtext;padding:0cm">.
To prevent this, when reading user input, always only use </span><code><span style="font-size:9pt;border:1pt none windowtext;padding:0cm">nextLine</span></code><span style="font-size:10.5pt;font-family:"Noto Sans",sans-serif;border:1pt none windowtext;padding:0cm">.
If you need an </span><code><span style="font-size:9pt;border:1pt none windowtext;padding:0cm">int</span></code><span style="font-size:10.5pt;font-family:"Noto Sans",sans-serif;border:1pt none windowtext;padding:0cm">, do
</span><u></u><u></u></p>
<div>
<pre style="vertical-align:baseline"><span class="m_-4664846296377142745gmail-hljs-type"><span style="font-family:"inherit",serif;color:rgb(255,123,114);border:1pt none windowtext;padding:0cm">int</span></span><code><span style="border:1pt none windowtext;padding:0cm"> </span></code><span class="m_-4664846296377142745gmail-hljs-variable"><span style="font-family:"inherit",serif;color:rgb(121,192,255);border:1pt none windowtext;padding:0cm">value</span></span><code><span style="border:1pt none windowtext;padding:0cm"> </span></code><span class="m_-4664846296377142745gmail-hljs-operator"><span style="font-family:"inherit",serif;color:rgb(121,192,255);border:1pt none windowtext;padding:0cm">=</span></span><code><span style="border:1pt none windowtext;padding:0cm"> Integer.parseInt(scanner.nextLine());</span></code><span style="font-family:"inherit",serif"><u></u><u></u></span></pre>
</div>
<p class="MsoNormal"><span style="font-size:10.5pt;font-family:"Noto Sans",sans-serif;border:1pt none windowtext;padding:0cm">instead of using
</span><code><span style="font-size:9pt;border:1pt none windowtext;padding:0cm">nextInt</span></code><span style="font-size:10.5pt;font-family:"Noto Sans",sans-serif;border:1pt none windowtext;padding:0cm">. Assume the following:
</span><u></u><u></u></p>
<div>
<pre style="vertical-align:baseline"><span class="m_-4664846296377142745gmail-hljs-type"><span style="font-family:"inherit",serif;color:rgb(255,123,114);border:1pt none windowtext;padding:0cm">Scanner</span></span><code><span style="border:1pt none windowtext;padding:0cm"> </span></code><span class="m_-4664846296377142745gmail-hljs-variable"><span style="font-family:"inherit",serif;color:rgb(121,192,255);border:1pt none windowtext;padding:0cm">scanner</span></span><code><span style="border:1pt none windowtext;padding:0cm"> </span></code><span class="m_-4664846296377142745gmail-hljs-operator"><span style="font-family:"inherit",serif;color:rgb(121,192,255);border:1pt none windowtext;padding:0cm">=</span></span><code><span style="border:1pt none windowtext;padding:0cm"> </span></code><span class="m_-4664846296377142745gmail-hljs-keyword"><span style="font-family:"inherit",serif;color:rgb(255,123,114);border:1pt none windowtext;padding:0cm">new</span></span><code><span style="border:1pt none windowtext;padding:0cm"> </span></code><span class="m_-4664846296377142745gmail-hljs-title"><span style="font-family:"inherit",serif;color:rgb(210,168,255);border:1pt none windowtext;padding:0cm">Scanner</span></span><code><span style="border:1pt none windowtext;padding:0cm">(System.in);<u></u><u></u></span></code></pre>
<pre style="vertical-align:baseline"><code><span style="border:1pt none windowtext;padding:0cm"><u></u> <u></u></span></code></pre>
<pre style="vertical-align:baseline"><code><span style="border:1pt none windowtext;padding:0cm">System.out.println(</span></code><span class="m_-4664846296377142745gmail-hljs-string"><span style="font-family:"inherit",serif;color:rgb(165,214,255);border:1pt none windowtext;padding:0cm">"Enter your age:"</span></span><code><span style="border:1pt none windowtext;padding:0cm">);<u></u><u></u></span></code></pre>
<pre style="vertical-align:baseline"><span class="m_-4664846296377142745gmail-hljs-type"><span style="font-family:"inherit",serif;color:rgb(255,123,114);border:1pt none windowtext;padding:0cm">int</span></span><code><span style="border:1pt none windowtext;padding:0cm"> </span></code><span class="m_-4664846296377142745gmail-hljs-variable"><span style="font-family:"inherit",serif;color:rgb(121,192,255);border:1pt none windowtext;padding:0cm">age</span></span><code><span style="border:1pt none windowtext;padding:0cm"> </span></code><span class="m_-4664846296377142745gmail-hljs-operator"><span style="font-family:"inherit",serif;color:rgb(121,192,255);border:1pt none windowtext;padding:0cm">=</span></span><code><span style="border:1pt none windowtext;padding:0cm"> scanner.nextInt();<u></u><u></u></span></code></pre>
<pre style="vertical-align:baseline"><code><span style="border:1pt none windowtext;padding:0cm">System.out.println(</span></code><span class="m_-4664846296377142745gmail-hljs-string"><span style="font-family:"inherit",serif;color:rgb(165,214,255);border:1pt none windowtext;padding:0cm">"Enter your name:"</span></span><code><span style="border:1pt none windowtext;padding:0cm">);<u></u><u></u></span></code></pre>
<pre style="vertical-align:baseline"><span class="m_-4664846296377142745gmail-hljs-type"><span style="font-family:"inherit",serif;color:rgb(255,123,114);border:1pt none windowtext;padding:0cm">String</span></span><code><span style="border:1pt none windowtext;padding:0cm"> </span></code><span class="m_-4664846296377142745gmail-hljs-variable"><span style="font-family:"inherit",serif;color:rgb(121,192,255);border:1pt none windowtext;padding:0cm">name</span></span><code><span style="border:1pt none windowtext;padding:0cm"> </span></code><span class="m_-4664846296377142745gmail-hljs-operator"><span style="font-family:"inherit",serif;color:rgb(121,192,255);border:1pt none windowtext;padding:0cm">=</span></span><code><span style="border:1pt none windowtext;padding:0cm"> scanner.nextLine();<u></u><u></u></span></code></pre>
<pre style="vertical-align:baseline"><code><span style="border:1pt none windowtext;padding:0cm"><u></u> <u></u></span></code></pre>
<pre style="vertical-align:baseline"><code><span style="border:1pt none windowtext;padding:0cm">System.out.println(</span></code><span class="m_-4664846296377142745gmail-hljs-string"><span style="font-family:"inherit",serif;color:rgb(165,214,255);border:1pt none windowtext;padding:0cm">"Hello "</span></span><code><span style="border:1pt none windowtext;padding:0cm"> + name + </span></code><span class="m_-4664846296377142745gmail-hljs-string"><span style="font-family:"inherit",serif;color:rgb(165,214,255);border:1pt none windowtext;padding:0cm">", you are "</span></span><code><span style="border:1pt none windowtext;padding:0cm"> + age + </span></code><span class="m_-4664846296377142745gmail-hljs-string"><span style="font-family:"inherit",serif;color:rgb(165,214,255);border:1pt none windowtext;padding:0cm">" years old"</span></span><code><span style="border:1pt none windowtext;padding:0cm">);</span></code><span style="font-family:"inherit",serif"><u></u><u></u></span></pre>
</div>
<p class="MsoNormal"><span style="font-size:10.5pt;font-family:"Noto Sans",sans-serif;border:1pt none windowtext;padding:0cm">When executing this code, you will be asked to enter an age, suppose you enter
</span><code><span style="font-size:9pt;border:1pt none windowtext;padding:0cm">20</span></code><span style="font-size:10.5pt;font-family:"Noto Sans",sans-serif;border:1pt none windowtext;padding:0cm">. However, the code will not ask you to actually input
a name and the output will be: </span><u></u><u></u></p>
<div>
<pre style="vertical-align:baseline"><code><span style="border:1pt none windowtext;padding:0cm">Hello , you are </span></code><span class="m_-4664846296377142745gmail-hljs-number"><span style="font-family:"inherit",serif;color:rgb(121,192,255);border:1pt none windowtext;padding:0cm">20</span></span><code><span style="border:1pt none windowtext;padding:0cm"> years old.</span></code><span style="font-family:"inherit",serif"><u></u><u></u></span></pre>
</div>
<p class="MsoNormal"><span style="font-size:10.5pt;font-family:"Noto Sans",sans-serif;border:1pt none windowtext;padding:0cm">The reason why is that when you hit the
</span><code><span style="font-size:9pt;border:1pt none windowtext;padding:0cm">enter</span></code><span style="font-size:10.5pt;font-family:"Noto Sans",sans-serif;border:1pt none windowtext;padding:0cm"> button, your actual input is
</span><u></u><u></u></p>
<div>
<pre style="vertical-align:baseline"><span class="m_-4664846296377142745gmail-hljs-number"><span style="font-family:"inherit",serif;color:rgb(121,192,255);border:1pt none windowtext;padding:0cm">20</span></span><code><span style="border:1pt none windowtext;padding:0cm">\n</span></code><span style="font-family:"inherit",serif"><u></u><u></u></span></pre>
</div>
<p class="MsoNormal"><span style="font-size:10.5pt;font-family:"Noto Sans",sans-serif;border:1pt none windowtext;padding:0cm">and not just
</span><code><span style="font-size:9pt;border:1pt none windowtext;padding:0cm">20</span></code><span style="font-size:10.5pt;font-family:"Noto Sans",sans-serif;border:1pt none windowtext;padding:0cm">. A call to
</span><code><span style="font-size:9pt;border:1pt none windowtext;padding:0cm">nextInt</span></code><span style="font-size:10.5pt;font-family:"Noto Sans",sans-serif;border:1pt none windowtext;padding:0cm"> will now consume the
</span><code><span style="font-size:9pt;border:1pt none windowtext;padding:0cm">20</span></code><span style="font-size:10.5pt;font-family:"Noto Sans",sans-serif;border:1pt none windowtext;padding:0cm"> and leave the newline symbol
</span><code><span style="font-size:9pt;border:1pt none windowtext;padding:0cm">\n</span></code><span style="font-size:10.5pt;font-family:"Noto Sans",sans-serif;border:1pt none windowtext;padding:0cm"> in the internal input buffer of
</span><code><span style="font-size:9pt;border:1pt none windowtext;padding:0cm">System.in</span></code><span style="font-size:10.5pt;font-family:"Noto Sans",sans-serif;border:1pt none windowtext;padding:0cm">. The call to
</span><code><span style="font-size:9pt;border:1pt none windowtext;padding:0cm">nextLine</span></code><span style="font-size:10.5pt;font-family:"Noto Sans",sans-serif;border:1pt none windowtext;padding:0cm"> will now not lead to a new input, since there
is still unread input left in </span><code><span style="font-size:9pt;border:1pt none windowtext;padding:0cm">System.in</span></code><span style="font-size:10.5pt;font-family:"Noto Sans",sans-serif;border:1pt none windowtext;padding:0cm">. So it will
read the </span><code><span style="font-size:9pt;border:1pt none windowtext;padding:0cm">\n</span></code><span style="font-size:10.5pt;font-family:"Noto Sans",sans-serif;border:1pt none windowtext;padding:0cm">, leading to an empty input. So every user
input is not only a number, but a </span><b><span style="font-size:10.5pt;font-family:"inherit",serif;border:1pt none windowtext;padding:0cm">full line</span></b><span style="font-size:10.5pt;font-family:"Noto Sans",sans-serif;border:1pt none windowtext;padding:0cm">.
As such, it makes much more sense to also use </span><code><span style="font-size:9pt;border:1pt none windowtext;padding:0cm">nextLine()</span></code><span style="font-size:10.5pt;font-family:"Noto Sans",sans-serif;border:1pt none windowtext;padding:0cm">,
even if reading just an age. The corrected code which works as intended is: </span>
<u></u><u></u></p>
<div>
<pre style="vertical-align:baseline"><span class="m_-4664846296377142745gmail-hljs-type"><span style="font-family:"inherit",serif;color:rgb(255,123,114);border:1pt none windowtext;padding:0cm">Scanner</span></span><code><span style="border:1pt none windowtext;padding:0cm"> </span></code><span class="m_-4664846296377142745gmail-hljs-variable"><span style="font-family:"inherit",serif;color:rgb(121,192,255);border:1pt none windowtext;padding:0cm">scanner</span></span><code><span style="border:1pt none windowtext;padding:0cm"> </span></code><span class="m_-4664846296377142745gmail-hljs-operator"><span style="font-family:"inherit",serif;color:rgb(121,192,255);border:1pt none windowtext;padding:0cm">=</span></span><code><span style="border:1pt none windowtext;padding:0cm"> </span></code><span class="m_-4664846296377142745gmail-hljs-keyword"><span style="font-family:"inherit",serif;color:rgb(255,123,114);border:1pt none windowtext;padding:0cm">new</span></span><code><span style="border:1pt none windowtext;padding:0cm"> </span></code><span class="m_-4664846296377142745gmail-hljs-title"><span style="font-family:"inherit",serif;color:rgb(210,168,255);border:1pt none windowtext;padding:0cm">Scanner</span></span><code><span style="border:1pt none windowtext;padding:0cm">(System.in);<u></u><u></u></span></code></pre>
<pre style="vertical-align:baseline"><code><span style="border:1pt none windowtext;padding:0cm"><u></u> <u></u></span></code></pre>
<pre style="vertical-align:baseline"><code><span style="border:1pt none windowtext;padding:0cm">System.out.println(</span></code><span class="m_-4664846296377142745gmail-hljs-string"><span style="font-family:"inherit",serif;color:rgb(165,214,255);border:1pt none windowtext;padding:0cm">"Enter your age:"</span></span><code><span style="border:1pt none windowtext;padding:0cm">);<u></u><u></u></span></code></pre>
<pre style="vertical-align:baseline"><span class="m_-4664846296377142745gmail-hljs-comment"><span style="font-family:"inherit",serif;color:rgb(139,148,158);border:1pt none windowtext;padding:0cm">// Now nextLine, not nextInt anymore</span></span><code><span style="border:1pt none windowtext;padding:0cm"><u></u><u></u></span></code></pre>
<pre style="vertical-align:baseline"><span class="m_-4664846296377142745gmail-hljs-type"><span style="font-family:"inherit",serif;color:rgb(255,123,114);border:1pt none windowtext;padding:0cm">int</span></span><code><span style="border:1pt none windowtext;padding:0cm"> </span></code><span class="m_-4664846296377142745gmail-hljs-variable"><span style="font-family:"inherit",serif;color:rgb(121,192,255);border:1pt none windowtext;padding:0cm">age</span></span><code><span style="border:1pt none windowtext;padding:0cm"> </span></code><span class="m_-4664846296377142745gmail-hljs-operator"><span style="font-family:"inherit",serif;color:rgb(121,192,255);border:1pt none windowtext;padding:0cm">=</span></span><code><span style="border:1pt none windowtext;padding:0cm"> Integer.parseInt(scanner.nextLine());<u></u><u></u></span></code></pre>
<pre style="vertical-align:baseline"><code><span style="border:1pt none windowtext;padding:0cm">System.out.println(</span></code><span class="m_-4664846296377142745gmail-hljs-string"><span style="font-family:"inherit",serif;color:rgb(165,214,255);border:1pt none windowtext;padding:0cm">"Enter your name:"</span></span><code><span style="border:1pt none windowtext;padding:0cm">);<u></u><u></u></span></code></pre>
<pre style="vertical-align:baseline"><span class="m_-4664846296377142745gmail-hljs-type"><span style="font-family:"inherit",serif;color:rgb(255,123,114);border:1pt none windowtext;padding:0cm">String</span></span><code><span style="border:1pt none windowtext;padding:0cm"> </span></code><span class="m_-4664846296377142745gmail-hljs-variable"><span style="font-family:"inherit",serif;color:rgb(121,192,255);border:1pt none windowtext;padding:0cm">name</span></span><code><span style="border:1pt none windowtext;padding:0cm"> </span></code><span class="m_-4664846296377142745gmail-hljs-operator"><span style="font-family:"inherit",serif;color:rgb(121,192,255);border:1pt none windowtext;padding:0cm">=</span></span><code><span style="border:1pt none windowtext;padding:0cm"> scanner.nextLine();<u></u><u></u></span></code></pre>
<pre style="vertical-align:baseline"><code><span style="border:1pt none windowtext;padding:0cm"><u></u> <u></u></span></code></pre>
<pre style="vertical-align:baseline"><code><span style="border:1pt none windowtext;padding:0cm">System.out.println(</span></code><span class="m_-4664846296377142745gmail-hljs-string"><span style="font-family:"inherit",serif;color:rgb(165,214,255);border:1pt none windowtext;padding:0cm">"Hello "</span></span><code><span style="border:1pt none windowtext;padding:0cm"> + name + </span></code><span class="m_-4664846296377142745gmail-hljs-string"><span style="font-family:"inherit",serif;color:rgb(165,214,255);border:1pt none windowtext;padding:0cm">", you are "</span></span><code><span style="border:1pt none windowtext;padding:0cm"> + age + </span></code><span class="m_-4664846296377142745gmail-hljs-string"><span style="font-family:"inherit",serif;color:rgb(165,214,255);border:1pt none windowtext;padding:0cm">" years old"</span></span><code><span style="border:1pt none windowtext;padding:0cm">);</span></code><span style="font-family:"inherit",serif"><u></u><u></u></span></pre>
</div>
<p class="MsoNormal"><span style="font-size:10.5pt;font-family:"Noto Sans",sans-serif;border:1pt none windowtext;padding:0cm">The
</span><code><span style="font-size:9pt;border:1pt none windowtext;padding:0cm">nextXXX</span></code><span style="font-size:10.5pt;font-family:"Noto Sans",sans-serif;border:1pt none windowtext;padding:0cm"> methods, such as
</span><code><span style="font-size:9pt;border:1pt none windowtext;padding:0cm">nextInt</span></code><span style="font-size:10.5pt;font-family:"Noto Sans",sans-serif;border:1pt none windowtext;padding:0cm"> can be useful when reading multi-input from
a single line. For example when you enter </span><code><span style="font-size:9pt;border:1pt none windowtext;padding:0cm">20 John</span></code><span style="font-size:10.5pt;font-family:"Noto Sans",sans-serif;border:1pt none windowtext;padding:0cm"> in
a single line.</span><u></u><u></u></p>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
</div>
</div>
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<div>
<p class="MsoNormal">On Thu, Nov 7, 2024, 11:36 AM Kenneth Fogel <<a href="mailto:kfogel@dawsoncollege.qc.ca" target="_blank">kfogel@dawsoncollege.qc.ca</a>> wrote:<u></u><u></u></p>
</div>
<blockquote style="border-top:none;border-right:none;border-bottom:none;border-left:1pt solid rgb(204,204,204);padding:0cm 0cm 0cm 6pt;margin-left:4.8pt;margin-right:0cm">
<div>
<div>
<p class="MsoNormal"><span lang="EN-US">The readln method introduced as part java.base is great because it includes the prompt for the input. The only shortcoming is that if the input must be a number
then you still need to employ a static class member such as Double.parseDouble() to make it a number:</span><u></u><u></u></p>
<p class="MsoNormal"><span lang="EN-US"> </span><u></u><u></u></p>
<p class="MsoNormal"><span lang="EN-US">// Currently</span><u></u><u></u></p>
<p class="MsoNormal"><span lang="EN-US">var loan = Double.parseDouble(readln("Loan: "));</span><u></u><u></u></p>
<p class="MsoNormal"><span lang="EN-US"> </span><u></u><u></u></p>
<p class="MsoNormal"><span lang="EN-US">// My delusional idea</span><u></u><u></u></p>
<p class="MsoNormal"><span lang="EN-US">var loan = readDbl("Loan: ");</span><u></u><u></u></p>
<p class="MsoNormal"><span lang="EN-US"> </span><u></u><u></u></p>
<p class="MsoNormal"><span lang="EN-US">I can think of reasons why this could be a bad idea. I taught my students to use a construct using Scanner to create a console input that passed my Beethoven
test (while humming Beethoven’s 5<sup>th</sup> symphony randomly strike keys on your keyboard as if you were playing the symphony and your code should just report invalid input and not an exception). If we had a readInt and/or readDbl they would not pass this
test. That Python would happily accept anything and then fail when the value was used in a calculation is no better. But, for learning Java could we have a readInt or readDbl alongside readln? I know that
</span>DataInputStream has such methods, but without a prompt and it must be attached to an input stream such as a file.<u></u><u></u></p>
<p class="MsoNormal"><span lang="EN-US"> </span><u></u><u></u></p>
<p class="MsoNormal"><span lang="EN-US">As always, just thinking out loud. Feel free to use my name in vain.</span><u></u><u></u></p>
<p class="MsoNormal"><span lang="EN-US"> </span><u></u><u></u></p>
<p class="MsoNormal"><span lang="EN-US">Ken</span><u></u><u></u></p>
<p class="MsoNormal"><span lang="EN-US"> </span><u></u><u></u></p>
<p class="MsoNormal"><span lang="EN-US"> </span><u></u><u></u></p>
</div>
</div>
</blockquote>
</div>
</div>
</div>
</div></blockquote></div></div>