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.

Results 1 to 6 of 6

Thread: Article: Valid User Input

  1. #1
    Forum VIP
    Join Date
    Jun 2011
    Posts
    381
    My Mood
    Busy
    Thanks
    49
    Thanked 112 Times in 95 Posts
    Blog Entries
    5

    Default Article: Valid User Input

    Computers are fascinating machines, but they're mostly a reflection of the people using them.
    -- Jeff Atwood


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default

    Good post Chris. Thanks.

  3. #3
    Junior Member
    Join Date
    Jul 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Nice post.Very helpful.

  4. #4
    Junior Member
    Join Date
    Aug 2011
    Posts
    1
    My Mood
    Bored
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    smart post chris.. hats off to u

  5. #5
    Forum VIP
    Join Date
    Jun 2011
    Posts
    381
    My Mood
    Busy
    Thanks
    49
    Thanked 112 Times in 95 Posts
    Blog Entries
    5

    Default Re: Article: Valid User Input

    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
    public static final String readString(String prompt, String error) {
      String input = readLine(prompt);
      if ((input.length() == 0) || (input == null) || (input.equals("\n"))) {
       System.out.println("\n" + error);
      }
     
      return input;  
     }

    with this
    public static final String readString(String prompt, String error) {
      String input = readLine(prompt);
      if ((input.length() == 0) || (input == null) || (input.equals("\n"))) {
       System.out.println("\n" + error);
       return readString(prompt, error);
      }
     
      return input;  
     }

  6. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default

    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.

    String lineTerminator = System.getProperty("line.separator");



    All in all, this is a good idea. Nicely done!
    Last edited by helloworld922; March 20th, 2012 at 10:08 AM.

Similar Threads

  1. Valid user input
    By ChristopherLowe in forum Java Programming Tutorials
    Replies: 1
    Last Post: June 21st, 2011, 05:53 PM
  2. User Input File Name
    By PineAppleKing in forum Java Theory & Questions
    Replies: 12
    Last Post: June 3rd, 2011, 11:23 AM
  3. Game requesting user input
    By Neo in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 3rd, 2011, 10:00 PM
  4. Allowing user to input variable.
    By That1guy in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 15th, 2010, 11:30 PM
  5. User Input Loop
    By cfmonster in forum Loops & Control Statements
    Replies: 7
    Last Post: August 24th, 2009, 02:52 PM