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.

2
  • 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. Commented Jun 6, 2015 at 4: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 Commented Jun 6, 2015 at 5:59

2 Answers 2

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:

Sign up to request clarification or add additional context in comments.

5 Comments

so if I use this, I dont need to create another class for users?
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<Integer, Object> usersMap = new HashMap<Integer, Object>(); usersMap.put(1238921,UsersObject); But I would recommend the one in my answer if you don't want to do something big.
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 :)
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.
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 :)
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.

2 Comments

do you want the whole GUI code? I can give it but its very long, tho :P
Yeah just post it in the OP

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.