Krish13287
Krish13287

Reputation: 198

Is there a way to create an object with object name from the value of a string

class states{

    List<String> cities = new ArrayList<String>();

    public void add_city(String current_city){
        this.cities.add(current_city);
    }
}

class add_values{

    public static void main(String [] args){
        System.out.println("Enter the state name : ");
        Scanner sc = new Scanner(System.in);
        String user_state = sc.nextLine(); // need to create an object with the name from a string value
        states user_state = new states();
        ....
        ....
    }
}

Is there a way to create an object for a class with the name from a string.

If, user enters the state name as "Michigan", an object is created with "Michigan" which is got from scanner. and cities can be added for the state Michigan.

Upvotes: 1

Views: 123

Answers (2)

Nayan
Nayan

Reputation: 1605

You can do something different using HashMap. Hope this will help to achieve your goal. Here is the code:

class States{

    List<String> cities = new ArrayList<String>();

    public void add_city(String current_city){
        this.cities.add(current_city);
    }
}

class add_values{

    public static void main(String [] args){
        HashMap<String, States> userStatesMap = new HashMap<String, States>();
        System.out.println("Enter the state name : ");
        Scanner sc = new Scanner(System.in);
        String user_state = sc.nextLine(); // need to create an object with the name from a string value
        userStatesMap.put(user_state, new states());

        ....
        ....
    }
}

Upvotes: 0

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285403

You will want to understand that objects have no names -- none, zero, zip. Yes, variables can have names, but that is not the same, since two or more variables can refer to the same object, and when that happens, which name is the name for the object? Again, neither/none since objects don't have names. As for variable names, they're way less important than you believe and almost don't exist in compiled code. What is most important are object references. Here an object can be associated with a String by means of a Map such as a HashMap<String, String> which is similar to an array or ArrayList that uses a String as its index rather than a number.

Now having said this, you can give your State class a String name field, and this may serve your purposes well.

i.e.,

import java.util.ArrayList;
import java.util.List;

public class State {
   private String name;
   private List<String> cities = new ArrayList<>();

   public State(String name) {
      this.name = name;
   }

   public String getName() {
      return name;
   }

   public void addCity(String city) {
      cities.add(city);
   }

   public List<String> getCities() {
      return cities;
   }

   @Override
   public String toString() {
      return "State of " + name + ", Cities: " + cities;
   }

}

Which can be run like:

public class StateTest {
   public static void main(String[] args) {
      State illinois = new State("Illinois"); 
      illinois.addCity("Chicago");
      illinois.addCity("Peoria");
      illinois.addCity("Springfield");

      System.out.println(illinois);
   }
}

Upvotes: 4

Related Questions