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: Generate UserID?

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Generate UserID?

    Hello, I'm learning how to use Java and I am not really understanding what I'm doing. I am trying to create a class (which I'm having trouble understanding alone), but in this class, I'm trying to make a username that contains the first three letters of the first and last name then random numbers after it. Then just a random six digit password. This is what I have so far:

    public class UserID {

    private static final int MAX = 6;

    private String firstName;
    private String lastName;
    private String user;
    private String password;

    public UserID (String first, String last){
    firstName = first;
    lastName = last;
    }

    public String getId()

    /**
    *
    * @return
    */
    {
    return user;
    }

    public String setID()
    {
    return user;
    }
    public String getPassword()

    {
    return password;
    }

    public boolean setPassword(String pw) {


    boolean isSet = true;
    if (pw.length() >= 6) {

    password = pw;
    isSet = false;
    }
    return isSet;
    }

    public String generateNewPassword(int pw) {

    pw = (int)(Math.random() * MAX) + 1;
    return password;
    }


    public String toString() {
    String output = "First name: " + firstName
    + " " + lastName + "\n";
    output += "User ID: " + user + "\n";
    output += "Password: " + password + "\n";
    output += "Would you like to change your password?"
    + "(Y - Yes, N - No) ";

    return output;
    }

    }


    I'm not asking for someone to do my homework, but could someone steer me in the right direction and tell me what I'm doing wrong here?

    Thank you1


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,105
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

    Default Re: Generate UserID?

    What does the program generate now?
    Show what it outputs and explain what is wrong with it.

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Generate UserID?

    Try using

    the String method subString with parameters (0,3) to get the first three letters of the first and last names.

    Either use the String method concat(String s) to put them all together or use the "+" operator to concat them together.

    Create a char array with values 0-9.

    char[] array = new char[10];
    char[0] = '0';
    char[1] = '1';
    etc
    char[9] = '9';

    Use the Random class (in java.util package)

    Random r = new Random();
    String password = "";
    for (int i = 0; i < 6; i++)
    {
    int rand = r.nextInt(10);
    password = password + String.valueOf(array[rand]);
    }

    If the 6 digits can include letters, both capital and lowercase as well as numbers, then you'll need about


    (26*2 = 52 + 10 = 62)
    spots in your array.
    Array will be size 62 instead of size 10.

    Fill in indexes 0 through 61. (Arrays start at index 0.)

    And you'll need to change the value in the nextInt() thing to 72. (It chooses random value between 0, inclusive, and the number in the brackets, exclusive.)
    Last edited by javapenguin; September 18th, 2011 at 08:48 PM.

  4. The Following User Says Thank You to javapenguin For This Useful Post:

    sukuiichuu (September 18th, 2011)

  5. #4
    Junior Member
    Join Date
    Sep 2011
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Generate UserID?

    Using A as String firstName and B as String lastName in Interactions, the output is currently:
    First name: A B
    User ID: null
    Password: null
    Would you like to change your password?(Y - Yes, N - No)

    Basically I am trying to figure out how to go about my User ID not null, but to make a user ID such as ab123 and how to make my Password not null, but a random six digit number.

  6. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,105
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

    Default Re: Generate UserID?

    Where do you assign values to the variables that you are printing and that are showing null?
    user and password
    If you do not give them values, they will be null.

  7. The Following User Says Thank You to Norm For This Useful Post:

    sukuiichuu (September 18th, 2011)

  8. #6
    Junior Member
    Join Date
    Sep 2011
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Generate UserID?

    Ahh, subString! Thank you javapenguin!

    Also, oh derp. I guess I do need to give them values. I think I understand what I need to do now, thank you Norm!

Similar Threads

  1. Generate GUI for web based application
    By Dev777 in forum Member Introductions
    Replies: 0
    Last Post: July 8th, 2011, 05:52 AM
  2. loop generate?
    By minotaurus in forum Loops & Control Statements
    Replies: 3
    Last Post: March 14th, 2011, 02:41 PM
  3. How do you generate javadoc?
    By javapenguin in forum Java IDEs
    Replies: 15
    Last Post: November 27th, 2010, 08:12 PM
  4. How to generate an ActionEvent progrematically
    By nasi in forum Java Theory & Questions
    Replies: 7
    Last Post: May 22nd, 2010, 12:31 PM