1

I want to call a method "Talk" from the class Person. Lets say I have a lot of objects, for a lot of persons. So if I want to let the user enter the name of the person that he wants to call the method, how do I store the user input in a variable and then use it to call the method like this:

Scanner name= new Scanner(System.in);
String input= name.next();
input.Talk();

Insted of doing:

 switch (input) {
        case "John":  John.Talk();
                 break;
        case "Alex":  Alex.Talk();
                 break;
        case "Albert":  Albert.Talk();
                 break;
        ...
}

I hope I explained myself good enough. I think it is a simple concept therefore there must be a solution.

3
  • 1
    you need to create the talk() method in the Person class. Right now you have no talk method (atleast didn't post one) and you should also post the code for the Person class(by the way, by convention in JAVA method and variable names start with a lowercase letter) Commented Aug 3, 2015 at 16:55
  • The bit some of the answers didn't quite explain is that you should separate your directory logic from your calling logic. Commented Aug 3, 2015 at 17:10
  • @peggy I am new with java, I have used some programming before but never with object oriented. I was just practicing with some objects and methods. The method talk() just prints a line. Person class is just a simple class with 1 variable and 1 method which is the Talk method. Commented Aug 3, 2015 at 19:40

3 Answers 3

6

You may want to use Map<String,Person>. In that map you put("Alex", AlexObject) and when user will write Alex you can simply get Person object stored in map with key Alex like

map.get(nameFromScanner).Talk()

But be careful since if map doesn't contain that name, get will return null so you will try to invoke Talk on null which will throw NullPointerException.

So you can try doing something like

Person p = map.get(nameFromScanner);
if (p != null){
    p.Talk();
}

Or since Java 8 we can use something like

Optional.ofNullable(map.get(nameFromScanner)).ifPresent(Person::Talk);
Sign up to request clarification or add additional context in comments.

4 Comments

How do I put the object inside the map? I will learn first how to use maps in java since I am new in java programming. But thanks, I hope it works.
@user3667660 First you need to create instance of class implementing Map interface like HashMap. Then you invoke put(key,value) on that map. So in your case you can use something like Map<String,Person> map = new HashMap<>(); map.put("John",John); map.put("Alex", Alex); (first argument is String, second is instance of Person class).
Oh thanks so much for the answe, it been a while since I asked this, and now it look so stupid to ask something like that, but hey I was a noob. Thanks anyway
@user3667660 No problem. We all started somewhere :)
0

I am not really sure what you are asking, but you could use an interface and a look up to find your correct Object at runtime.

Assuming your Persons are stored in a collection and can be found using a "predicate" you can use streams and filters to handle this:

Optional<Person> op = lst.stream().filter(person -> person.firstName.equals(input.getText())).findFirst();
if(op.isPresent()){
    op.get().talk();
}    

Comments

0

You could use the factory design pattern for getting your objects.

2 Comments

This is closer to a Multiton than a Factory.
Yes, you are right. I haven't used multiton until now. Thanks

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.