0
public class CHECK {
public CHECK(){
    String []wrkrs = {"Денис", "Саша", "Наталья", "Анатолий", "Юра", "Коля", "Катя", "Дима", "Антон","Тамара"};
    int [] wrkrsPhone = {22626,22627,22628,22629,22630,22631,22632,22633,22634,22635};
    String a = JOptionPane.showInputDialog(null, "Hello,friend!Do you wanna know, is that guy at work?Enter name:");


    if(Arrays.asList(wrkrs).contains(a)){
        JOptionPane.showMessageDialog(null, "That guy is at work!");
        JOptionPane.showMessageDialog(null, "calling " + wrkrsPhone[wrkrsPhone.toString().indexOf(a)]);
    }else{
        JOptionPane.showMessageDialog(null, "Такого сотрудника нет!");
    }
}

I have two arrays, which contain ints and strings. As you can see, I want to add the number of elements in the string array (for example, wrkrs number 3) to the int array, called wrkrs phone. How can i do it? I tried IndexOf, but it doesn't work.

The output, i want is something like:

Enter name:
Юра
That guy is at work!
Calling Юра + wrkrsPhone(Юра).
0

2 Answers 2

5

A better solution would be to have a Worker class that would contain the worker's name and their phone number.

Then you can use a HashMap<String,Worker> instead of your arrays to store the data.

This makes the search more efficient :

Map<String,Worker> workersMap = new HashMap<>();
workersMap.put ("Денис", new Worker ("Денис", 22626));
...
Worker worker = workersMap.get(a);
if (worker != null) {
    call (worker.getPhone()); // or do whatever you want to do with the phone number
}

This is more efficient than Arrays.asList(wrkrs).contains(a), which performs linear search on the List.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I haven't worked with MAP, but i'll do it soon)
-1
...
List list = Arrays.asList(wrkrs);
if(list.contains(a)){
    JOptionPane.showMessageDialog(null, "That guy is at work!");
    JOptionPane.showMessageDialog(null, "calling " + wrkrsPhone[list.indexOf(a)]);
}
...

U better use a Map or extract a class Contact which have name and phone property but i think this is what u looking for :)

1 Comment

Thank you! I'll try it!)

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.