Reputation: 37
I am doing a MOOC and am supposed to return a number that is associated with a name. The number name combo is held in an object called Phonebook that is an ArrayList. The arrayList holds information of Person, a class that I created. I need to perform getNumber() on the Phonebook object but I can't since getNumber() only works on objects of type Person.
package problem94_phonebook;
import java.util.ArrayList;
public class Phonebook {
private ArrayList<Person> phonebook;
public Phonebook(){
this.phonebook = new ArrayList<Person>();
}
public String searchNumber(String name){
if (this.phonebook.contains(name)){
return this.phonebook.Person.getNumber(); // here is the problem
}
}
}
package problem94_phonebook;
import java.util.ArrayList;
public class Person {
private String Name;
private String Numb;
private ArrayList<String> Phonebook;
public Person(String name, String numb){
this.Name = name;
this.Numb = numb;
this.Phonebook = new ArrayList<String>();
}
public String getName() {
return Name;
}
public String getNumber() {
return Numb;
}
public String toString(){
return this.Name +" " +"nummber: " + this.Numb;
}
public void changeNumber(String newNumber){
this.Numb = newNumber;
}
public void add(String name, String number){
this.Phonebook.add(name);
this.Phonebook.add(number);
}
public void printAll(){
for(String i : this.Phonebook){
System.out.println(i);
}
}
}
Upvotes: 1
Views: 757
Reputation: 1478
EDIT
.contains
won't work since .contains
Returns true if and only if this list contains at least one element e such that (o==null ? e==null : o.equals(e)).
(More info on how .contains works here)
which means that unless your class overrides the .equals
method to compare the names this part -> o.equals(e) will always return false since .equals
is comparing two different objects. (more info on how .equals work here)
Thus if you really want to use .contains
you need to override .equals
method in your Person
class. But it still would not work since your second problem is you are accessing an element of an array list wrong.
But since you are a beginner, I suggest that you just try changing your method to this:
public ArrayList<String> searchNumber(String name){
ArrayList<String> result = new ArrayList<>(); // list incase of persons with the same name
for (Person p : phonebook){ // iterate through the array
if (p.getName().equals(name)){ // check if the current person's name is equal to anme
result.add(p.getNumber()); // add the person's phone number
}
}
return result ;
}
Also I noticed that you have an attribute Phonebook
in your Person
class. It is just an array of Strings but I think it is better if you change it to an array of Phonebook
so that you can make use of your class Phonebook
. This will be your new Person
class.
package problem94_phonebook;
import java.util.ArrayList;
import Phonebook;
public class Person {
private String Name;
private String Numb;
private ArrayList<Phonebook> phonebook; // updated
public Person(String name, String numb){
this.Name = name;
this.Numb = numb;
this.phonebook= new ArrayList<Phonebook>();// updated
}
public String getName() {
return Name;
}
public String getNumber() {
return Numb;
}
public String toString(){
return this.Name +" " +"nummber: " + this.Numb;
}
public void changeNumber(String newNumber){
this.Numb = newNumber;
}
public void add(Person personToBeAdded){ // changed
boolean isPresent = false;
// check if person to be added is already in the phonebook to avoid duplicates
for (Person person:this.phonebook){
if (person.getName().equals(personToBeAdded.getName()) && person.getNumber().equals(personToBeAdded.getNumber())){
isPresent = true;
break;
}
}
if (!isPresent){
this.phonebook.add(personToBeAdded);
}
}
public void printAll(){
for(Person person : this.phonebook){
System.out.println(person.toString());
}
}
}
Upvotes: 0
Reputation: 191681
You could use streams for this, but this has a subtle bug if there's two of the same name in the list, it only gets the first number
return this.phonebook.stream()
.filter(p -> p.getName().equals(name))
.map(Person::getNumber)
.findFirst().orElse(null);
Upvotes: 1
Reputation: 2357
you can try with this
for (Person person : phonebook)
{
if(person.getName().equals(name)){
return person.getNumber();
}
}
for use this you need to change return type of String searchNumber(String name)
String
to ArrayList<String>
Upvotes: 1
Reputation: 1291
After looking your code, change like below to get the number for a name from person:
public String searchNumber(String name){
for(int i=0;i<this.phonebook.size();i++){
if(this.phonebook.get(i).getName().equals(name)){
return this.phonebook.get(i).getNumber();
}
}
return null;
}
i hope your problem will get resolve if any need please let me know.
Upvotes: 0