0

When creating e.g an address book I also want a method for searching it. Imagine I go for an object like this:

public class someone {

private static someone [] addressBook = new someone[150];
private String firstName;
private String lastName;
private String address;
    //...

public someone(String firstName, String lastNameame, String adress/*,...*/){
    this.Vorname=firstName;
            //...
}

Now I'm planning on searching for someone using user input and Scanner. First, the user should declare for what (s)he is searching in gerneral:

public static void searchSomeone(){

System.out.println("What do you want to search for?: ");
Scanner sc1 = new Scanner(System.in);
String userInput1 = sc1.next(); // e.g. userInput1="firstName"

Second, the user is asked for the exact value (s)he is searching for:

System.out.println("Enter a " + userInput1 + ": ");
Scanner sc2 = new Scanner(System.in);
String userInput2 = sc2.next(); // e.g. userInput2="Peter"  

    for (int i=1;i<150;++i){
            if(getAddressBook[i].**"Value of userInput1"**.contains(userInput2)){ // here is my problem!*
                    System.out.println("Congrats, we've found someone! ");
        }
    }
}
  • Is there a way to use the value of the input as a call for the member variable?

In this case the argument should 'become'

if(getAddressBook[i].firstName.contains(userInput2)){}

I tried to implement another method like getInputValue that simply returns the String but it didn't work here. If there is no such solution I will have to include code for every member variable separately which I really dont want to!

Hope you get my idea! And please be kind I'm quite new to forums. :P

4

1 回答 1

0

仅供参考
我想我在这里尝试做的事情在 Java 中是不可能的。然而,Visual Basic 提供了一个“var”关键字和一个叫做“generics”的东西。对于遇到相同问题的每个人,都可以在那里找到解决方案。

于 2014-11-12T08:38:15.547 回答