How about a readln for numbers?

Kenneth Fogel kfogel at dawsoncollege.qc.ca
Thu Nov 7 16:33:54 UTC 2024


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:

// Currently
var loan = Double.parseDouble(readln("Loan: "));

// My delusional idea
var loan = readDbl("Loan: ");

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 5th 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 DataInputStream has such methods, but without a prompt and it must be attached to an input stream such as a file.

As always, just thinking out loud. Feel free to use my name in vain.

Ken


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/discuss/attachments/20241107/b460a2c2/attachment.htm>


More information about the discuss mailing list