UserData utility, Java

In my recent Java projects I’ve been finding myself hitting the user up for input, now during the development phase of these projects everything is command prompt, so here is a useful little userdata utility that I’ve been using, and as I run into other types of input I will be adding to it.

UserData.java


import java.util.Scanner;

/**
 * Gives us various methods for getting info from the user.
 *
 * @author Michael Avila
 * @version 1.0
 */
public class UserData
{

        private Scanner reader;
        
        /**
         * Constructor
         */
        public UserData()
        {
                reader = new Scanner( System.in );
        }

        /**
         * Asks the user a question, returns their response as a string
         */
        public String prompt ( String statement )
        {
            System.out.println( statement );
            return reader.nextLine();
        }

        /**
         * Asks the user a yes or no question, returns either Y or N
         */
        public boolean askYN( String question )
        {
            System.out.println(question + "[Y/N]");

            while (true)
            {
               String response = reader.nextLine();

               if (response.equals("Y"))
               {
                          return true;
               } else if (response.equals("N"))
               {
                          return false;
               }
               System.out.println(question + "[Y/N]");
               System.out.println("Please enter either [Y]es or [N]o");
            }
        }
}

And here is a little TestDrive application for seeing how it works…

UserDataTest.java,


public class UserDataTest
{
   /**
    * Entry-point
    */
   public static void main( String[] args )
   {
      // new instance of UserData
      UserData uData = new UserData();

      // asks a yes or no question, returning the answer as a boolean
      boolean likes = uData.askYN( "Do you like this class?" );
      System.out.println( likes );

      if (likes)
      {
         // prompts the user with either a question or statement, then returns their feedback.
         String response = uData.prompt( "What is one thing you like about this class?" );
         System.out.println( response );
      }
   }
}

This code isn’t the greatest thing in the world, but it has helped quicken development time for me, and I’m sure it will for you.

If you have any suggestions as to methods that could be added, go ahead and comment them, and I’ll try putting it together when I can. Take Care.

Great idea! I find myself doing this a lot, but just kind of learning to “shut off” my brain and typing it all up anyway, haha. Good idea for development stage!

I went through and added up some methods for other primitive types. You can add more for stuff like BigInteger and BigDecimal if you ever use that, but I didn’t. Only askYN and askChar need to be checked for validity, because Scanner will ONLY take the other values when you use the correct methods (like it will only take floats when you say nextFloat()). Anyway, here are my additional methods, thanks for the idea!

import java.util.Scanner;

public class UserData {
    private Scanner s;  // user input
    private String a;   // string checking
    private char c;     // char checking
    
    /** constructor */
    public UserData() {
        s = new Scanner(System.in);
    }
    
    /** asks for string input */
    public String prompt( String q ) {
        System.out.print(q);
        return s.nextLine();
    }
    
    /** asks yes/no question */
    public boolean askYN( String q ) {
        do {
            System.out.print(q + " [Y/N]: ");
            a = s.next();
            
            if(a.equalsIgnoreCase("y")) return true;
            else if(a.equalsIgnoreCase("n")) return false;        
        } while(true);
    }
    
    /** asks question for char value (a-z) */
    public char askChar( String q ) {
        do {
            System.out.print(q);            
            c = s.next().charAt(0);
            
            if(Character.isLetter(c)) return c;
        } while(true);
    }    
    
    /** asks for integer value */
    public int askInt( String q ) {
        System.out.print(q);
        return s.nextInt();
    }
    
    /** asks for double value */
    public double askDbl( String q ) {
        System.out.print(q);
        return s.nextDouble();
    }
    
    /** asks for float value */    
    public float askFlt( String q ) {
        System.out.print(q);
        return s.nextFloat();
    }
    
    /** asks for long value */
    public long askLong( String q ) {
        System.out.print(q);
        return s.nextLong();
    }
    
    /** asks for short value */
    public short askShort( String q ) {
        System.out.print(q);
        return s.nextShort();
    }
    
    
}

And then the demo class

public class UserData_demo {

    public static void main(String[] args) {
        UserData ud = new UserData();
        
        String s = ud.prompt("Gimme a string: ");
        boolean b = ud.askYN("Gimme Y or N");
        char c = ud.askChar("Gimme a char: ");
        int i = ud.askInt("Gimme an integer: ");
        
        System.out.println("
String: "+s+"
Boolean: "+b+"
Character: "+c+"
Integer: "+i);
    }

}

yeah, this is what I’m talking about. I just put this together, so I am adding to it as I need to. I was hoping someone would jump in. So we can have something that we didn’t have to sit here and make. Thanks for the additions.

TakeCare.
_Michael