Samuel
Samuel

Reputation: 21

How to check if an input string is the name of an existing object?

I'm trying to have the user input a the name of an object through a JFrame text field (txtLlave), and then check if said input matches the name of an instance of a class (Estudiante). In this case, I created est1 as an object of class Estudiante. I want the program to only accept the input if the user inputs "est1" or the name of another object of class Estudiante. If it's not an object of that class, it should ask to try again. I'm having trouble checking if an object with the name that's been input exists.

Alright so now I know objects don't have names, but then how do I let the user decide which object they want to use? Say that I have two objects of type Estudiante, est1 and est2. How do I let the user pick between the two?

I tried using instanceof directly with the string to see if it would work, but no. Also tried converting the string to an object and then check if said object is an instance of a class but it isn't working either.

Estudiante est1 = new Estudiante("Samuel",21);

if (txtLlave.getText() instanceof Estudiante) {

}

Upvotes: 2

Views: 888

Answers (2)

CryptoFool
CryptoFool

Reputation: 23119

Objects have no inherent names. Classes do, but not instances of a class. You need to impose your own naming mechanism on objects if you want to be able to look them up by name, like by putting them in a Map, where the keys in the map are object names and the values in the map are the objects themselves. Once you have an object, you can check its type. But how you match up an object to some string input by a user is totally up to you.

Here's an example of using a Map to impose a naming system on a series of object:

import java.util.HashMap;
import java.util.Map;

class Estudiante {

    private String name;
    private int age;

    public Estudiante(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String toString() {
        return "Name: " + name + "  Age: " + age;
    }
}

public class Test {

    public static void main(String... args) {

        Map<String, Estudiante> estudiantes = new HashMap<>();

        estudiantes.put("est1", new Estudiante("Samuel",21));
        estudiantes.put("est2", new Estudiante("John",24));
        estudiantes.put("est3", new Estudiante("Joe",33));

        String name;

        // name = txtLlave.getText()
        name = "est2";
        if (estudiantes.containsKey(name)) {
            System.out.println("Found " + name + ": " + estudiantes.get(name));
        }
        else {
            System.out.println("No object with name: " + name);
        }

        // name = txtLlave.getText()
        name = "xxx";
        if (estudiantes.containsKey(name)) {
            System.out.println("Found " + name + ": " + estudiantes.get(name));
        }
        else {
            System.out.println("No object with name: " + name);
        }
    }
}

Result:

Found est2: Name: John  Age: 24
No object with name: xxx

Upvotes: 2

FlutterDashie
FlutterDashie

Reputation: 108

When you create an instance of an object (new Estudiante(...)), it does not automatically add it to any sort of collection or lookup. As such, to determine if a student already exists, you would want to add it to some sort of collection, and add a way to look things up in it. One example of this is a HashMap. You could implement this in a manner similar to HashMap<String, Estudiante> dictionary = new HashMap<String, Estudiante>();. After that, you would need to add new records manually: dictionary.put("est1",est1); Thereafter, you could check the HashMap for that name using if(dictionary.containsKey(textLlave.getText())) {...}.

Upvotes: 0

Related Questions