You can view the page at http://www.javaprogrammingforums.com...lid-user-input
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
You can view the page at http://www.javaprogrammingforums.com...lid-user-input
Computers are fascinating machines, but they're mostly a reflection of the people using them.
-- Jeff Atwood
Good post Chris. Thanks.
Nice post.Very helpful.
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
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!
Last edited by helloworld922; March 20th, 2012 at 09:08 AM.