-4

Let's say I have this class below:

    public class Player{
}

I could make a new instance of it like this:

Player someone = new Player();

Now I have a .txt file with each line as a potential player:

george
joel
kate
...

I can read these lines and assign them to a String like this:

String name = "george";

How could I make a new Player with the name of "george"? For example, I would like to create "george" down below.

Player name.toClassName = new Player();

One solution I found would be:

if(name == "george"){
    Player george = new Player();
}
if(name == "joel"){
    Player joel = new Player();
}
if(name == "kate"){
    Player kate = new Player();
} ...

But this above just looks stupid. (By the way I already have a private name; inside the Player class, I am assigning those Strings to them after the instances are made, I can get and set them etc. but that is not what I would like to manipulate with here. I'm just interested if you could make new instances this way.

Hydraxia
  • 81
  • 2
  • 9
  • 3
    create a constructor with name `public Player(String name){this.name = name;}` then you can create `Player george = new Player("george");`... – Youcef LAIDANI Jun 28 '18 at 20:18
  • You should use a `HashMap`. – SLaks Jun 28 '18 at 20:19
  • 2
    what is the benefit of this? how could you ever call variables with names you do not know of yet? – Sam Jun 28 '18 at 20:19
  • 3
    Possible duplicate of [Assigning variables with dynamic names in Java](https://stackoverflow.com/questions/6729605/assigning-variables-with-dynamic-names-in-java) – GBlodgett Jun 28 '18 at 20:19
  • 2
    You don't want this. The variable name is there to let you refer to it in the code. It doesn't need to (and should not) reflect user input. – khelwood Jun 28 '18 at 20:23
  • Possible duplicate of [basic constructor usage in java](https://stackoverflow.com/questions/19747784/basic-constructor-usage-in-java) – the_storyteller Jun 28 '18 at 20:23
  • Thank you for the answers, and sorry I tried really but couldn't find the answer to it. The one you linked about the dynamic variables is helpful. I'm afraid though I couldn't use an array or a List because they use positions too, is that right? If I had an array of Player[], I could call Player[0], Player[1] etc. but then I couldn't refer to them by their names – Hydraxia Jun 28 '18 at 20:33
  • You could store them in an array. You probably don't want to reference them by their names. – theMaestro73 Jun 28 '18 at 20:37
  • But I would like to reference them by their names, sorry I really believe you it wouldn't be useful, I'm still interested if there is any way to do it – Hydraxia Jun 28 '18 at 20:42
  • @Hydraxia if you want to reference them by their names, why store the names in a separate txt file? Just create the variables with whatever names you want. – theMaestro73 Jun 28 '18 at 20:50
  • @theMaestro73 I originally wanted to mass produce those Player instances with a for loop, and I stored the list of names in a separate file so the code doesn't look messy, like if I had thousands of unique names. The number of those names can be different, because I have more files, each has X number of unique names. – Hydraxia Jun 28 '18 at 20:57
  • @Hydraxia, yes. That would be better. Like you said, loop through and create the players using the names from the file. Why do you want to reference the players by their names in code? – theMaestro73 Jun 28 '18 at 21:00
  • @theMaestro73 Thank you very much, I really appreciate your help. I wanted to reference to them by their names because then I could manipulate them easier. If they were stored in an Array, I wouldn't know which position represents which Player, same with Lists, as different files are loaded with different positions, names etc. – Hydraxia Jun 28 '18 at 21:11

2 Answers2

1

Your variable name shouldn't have anything to do with the instance it represents. Your programming should be abstract enough so that if you changed that txt file, you don't have to change any code.

First, your Player class should accept a name in the constructor (there are tons of ways to do this, this is just the simplest).

public class Player {
    private String name;

    public Player(String inName) {
        name = inName
    }
}

Then when you create your players, don't use the names as the name for the variable.

Player player = new Player(name);

However you're reading the .txt, you can create one Player for each line, or something like that.

theMaestro73
  • 134
  • 2
  • 11
  • Yes that is exactly what I want to do, each line should represent a Player. Thank you for your explanation – Hydraxia Jun 28 '18 at 20:45
1

I don't like the idea of creating the variable name based on the player's name like you mentioned in your question. If you ever need to know the name of the Player then you would just access the name variable. But this code will allow you to set the Player name field when the object is created by using a constructor.

 public class Player{

     Private string name;

     Player(String name)
      {
        this.name - name;
      }

}

Player player= new Player("joel");
Matthew S.
  • 321
  • 1
  • 9
  • 22
  • Thank you, I see what you mean. And do you think there could be a situation where it is still useful? Like if I had a big list of 100.000 names. Then I could find "joel" by using getPlayerName() for example. If the name is correct and it is "joel", then my program would do some calculations to it. But what if "joel" is the 79.544th in that big list of names? Wouldn't it save time and be easier if I knew I want to do something with "joel"? Or isn't that how it works? I'm really new, and this seems logical now to me, I'm trying to understand how coding works. Thank you – Hydraxia Jun 28 '18 at 20:40
  • 1
    Variable names are for your benefit while you are coding. Once the program is running, the unique variable names based on a person's name are no longer help you. If you had a list of Players, you will have to perform a search and /or iterate through the list in order to find the entries that you are looking for anyways. When a program is in execution, you are never going to base an if statement or a loop on a variable name. The decision will be based on the value inside the variable. – Matthew S. Jun 28 '18 at 20:48