0

I'm trying to make a Sign Up window on JFrame Form in NetBeans. I have created a class named users (in the same package) that has a username as String and password as char[].

When the username and password are filled, and Sign Up button is pressed, I want to make a new object in the class 'users' so that it's name is taken from username textfield itself.

I want to dynamically create object whose names have been taken from a string.

eg: If I put value of Username as "guy", then create users.guy which has a String = "guy" and a char[] as password.

package login;

public class users {
    private char[] password;
    String username;

    users() {
        username = "anonymous";
        password = null;
    }

    users(String u, char[] p) {
        username = u;
        password = p;
    }

    void putdata() {
        System.out.println(username);
        System.out.println(password);
    }
}

Thanks in advance.

blablaguy
  • 11
  • 4
  • I actually put up a photo to describe it best, but I'm new, and they wont let me. I want to dynamically create object whose names have been taken from a string. Thank you. – blablaguy Jun 06 '15 at 04:16
  • Generally speaking, the name of "variable" or "object" is irrelevant. You can use a `Map` of some kind to "key" a object to a name – MadProgrammer Jun 06 '15 at 05:59

2 Answers2

1

If I understand good your question what you want to do is a mapping. In Java there's a lot of ways for do that.

I would recommend you the use of HashMap, it's simple and efficient. There's a simple example.

String userYayotrón = "Yayotrón";
char[] passwordYayotrón = "contraseña".toArray();
Map<String, char[]> usersMap = new HashMap<String, char[]>();
//This hashmap will take two values, the KEY which identifies the VALUE. The key is the first one, which I define as String. I will use it for save the User's name. And the value will be the password.
usersMap.put(userYayotrón,passwordYayotrón);

Now, you can use this map for a lot of things. For example:

usersMap .get(userYayotrón); // This will return an char[] with the password of Yayotrón.
usersMap .size(); // How many users do you have in this map.

Also I highly recommend read the following question related:

Community
  • 1
  • 1
Yayotrón
  • 1,759
  • 16
  • 27
  • so if I use this, I dont need to create another class for users? – blablaguy Jun 06 '15 at 04:34
  • Yes. You can map in there how many users do you want. Also you can do multiple things like create an object User which have Name and Password and then Map the Object with an unique ID like this Map usersMap = new HashMap(); usersMap.put(1238921,UsersObject); But I would recommend the one in my answer if you don't want to do something big. – Yayotrón Jun 06 '15 at 04:36
  • its just like a chatapp, when you want to create a new user, you can create it by getting input from the GUI textfields. That's what I want, nothing too complicated. But thanks for the effort :) – blablaguy Jun 06 '15 at 04:40
  • oh, and after creating, I wanted to store it in a file, so that next time you login, it searches for your username, compares password and lets you in. – blablaguy Jun 06 '15 at 04:43
  • Oh, then try to keep it simple. Read the first question I suggested to you, maybe you'll find something better for your case, if not you will learn something incredible useful :) – Yayotrón Jun 06 '15 at 04:51
0

Create a new instance of the users class passing the username and password like so:

new users(userTextField.getText(), userPasswordField.getText().toCharArray())

If you include the code for the GUI itself I'd be able to give you a more direct answer/solution.

Jonathan Beaudoin
  • 2,158
  • 4
  • 27
  • 63