0

how would i change the name of a object using user input. for ex. i am asking the user to input their id as a string. i want to then use that to create a constructor.

ex.:

RefUnsortedList<Patients> NewList = new RefUnsortedList<Patients>();
Patients entry1=null;
System.out.println("Please enter patient's ID: ");
            String TargetID = scan.next();

I want to set

Patients entry1 = null;

to make it

Patients "the user input here" = null;
1
  • i think that isn't a good programming practice. users shouldn't name variables. better create a property for that. Commented Apr 7, 2014 at 14:29

3 Answers 3

1

There are no dynamic variables in java, they have to be declared in the source code.

You could try using a Map and assigning each instance of a variable a key.

Map patientMap = new HashMap();
patientMap.put("entry1", new Patients());
patientMap.put("the user input here", new Patients());

Then when you want to retrieve the patient you can use:

Patients patient = patientMap.get("the user input here");
Sign up to request clarification or add additional context in comments.

Comments

1

What you actually want to do is:

Map<String, Patient> patients = new HashMap<>();
patients.put("entry1", /* [insert Patient object here] */);

Things to note:

  • The class to represent a patient should be named Patient, not Paitents. A class should be named for its instances, not their collection.

  • It is meaningless to set the value in a map to null, unless you are using a special type of map that allows a null key (and makes it meaningfully different from not having an entry for that key).

3 Comments

There is no .set method for the Map class, it is .put to add objects to a map
@MatthewWilson Thanks, I mistyped.
And you mistyped patient and patients in your code example.
0

I am assuming you are doing something like this:

Your Patient Class:

public class Patient {
    private String patientID;

    public Patient(String patientID) {
        this.patientID = patientID;
    }

    public String getPatientID() {
        return patientID;
    }

    public void setPatientID(String patientID) {
        this.patientID = patientID;
    }
}

...and your class that you are using for running the console:

public class Main {

    public Main() {
    }

    public static void main(String[] args) {
        Scanner console = new Scanner(System.in);
        System.out.println("System is ready to accept input, please enter ID : ");
        String ID = console.nextLine();
        Patient patient = new Patient(ID);
        //do some fancy stuff with your patient
    }

}

This would be a very basic example.

As you are learning to code, be sure to really think about how to name your classes. Calling your class "Patients" would make me expect you are holding a collection of "Patients" inside each instance of this java class, rather than a single "Patient" per instance.

Regarding the latest answers including Maps, the updated "Main" class could look like this:

public class Main {
    static Map<String, Patient> patients = new HashMap<String, Patient>();

    public Main() {
    }

    public static void main(String[] args) {
        Scanner console = new Scanner(System.in);
        System.out.println("System is ready to accept input, please enter ID : ");
        String id = console.nextLine();
        patients.put(id, new Patient(id));
    }
}

Comments

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.