You can view the page at http://www.javaprogrammingforums.com...lid-user-input
Printable View
You can view the page at http://www.javaprogrammingforums.com...lid-user-input
Good post Chris. Thanks.
Nice post.Very helpful. =D>
smart post chris.. hats off to u :)
Big thank you to TJStrech for a bug fix in the readString function.
Symptom
Pressing enter without any input in the readString function causes the function to return instead of re-prompting.
Cause
Missing recursive call
Fix
Replace this function
Code Java:
with this
Some minor suggestions:
You might want to consider renaming the class to something like ConsoleTools. It's pretty bad to write a class with the same name as one in the Java API.
I don't really see much point of declaring a static final method since static methods can't be overwritten anyways. The only thing it does is prevent you from overloading a static method with the same name and parameters in inheriting classes.
You may want to look into using the Scanner class instead of trying to roll your own input reading method in readLine().
Personally I have some slight reservations about using recursion here instead of using while loops. While loops can handle an infinite number of miss-inputs from the user, but with enough recursive calls the user can get your program to crash due to a stack overflow. In practice this probably won't happen, but just recognize that it could.
If cross-platform support is your goal, in readLine() you may want to consider ending the line with the system line separator property. If you use the Scanner's nextLine() method it will do this for you.
All in all, this is a good idea. Nicely done!