Reputation: 71
I have an Object of AccountInfo which is having String type phone number. I need to compare each phone number within the list and return true if all the phone numbers are the same and return false if any one of them is different.
Below is the code snippet, The problem is I am always getting true value even if phone numbers are different. I know it is pretty basic but any help would be highly appreciated.
import java.util.Objects;
public class AccountInfo {
String accountName;
String phoneNumber;
public AccountInfo( String accountName, String phoneNumber) {
this.accountName = accountName;
this.phoneNumber = phoneNumber;
}
public String getAccountName() {
return accountName;
}
public void setAccountName(String accountName) {
this.accountName = accountName;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
AccountInfo that = (AccountInfo) o;
return Objects.equals(accountName, that.accountName) && Objects.equals(phoneNumber, that.phoneNumber);
}
@Override
public int hashCode() {
return Objects.hash(accountName, phoneNumber);
}
@Override
public String toString() {
return "AccountInfo{" +
"accountName='" + accountName + '\'' +
", phoneNumber='" + phoneNumber + '\'' +
'}';
}
}
And here is my main class
import java.util.ArrayList;
import java.util.List;
public class TestMain {
public static void main(String[]args){
List<AccountInfo> accInfoList = new ArrayList<AccountInfo>();
List<AccountInfo> accountInfoList =populateList(accInfoList);
System.out.println(accountInfoList);
boolean flag = isSamePhoneNumber(accInfoList);
if(flag) {
System.out.println("All Phone numbers are same");
}
else{
System.out.println("Some of the phone numbers are different");
}
}
public static List<AccountInfo> populateList(List<AccountInfo> accountInfoList){
AccountInfo accountInfo1 = new AccountInfo("Rohit Sharma", "123456");
AccountInfo accountInfo2 = new AccountInfo("Mayank Singh", "123456");
AccountInfo accountInfo3 = new AccountInfo("Sam Singh", "12378456");
AccountInfo accountInfo4 = new AccountInfo("Rahul Sharma", "123456");
accountInfoList.add(accountInfo1);
accountInfoList.add(accountInfo2);
accountInfoList.add(accountInfo3);
accountInfoList.add(accountInfo4);
return accountInfoList;
}
public static boolean isSamePhoneNumber(List<AccountInfo> accountInfoList){
boolean isSamePhoneNumberFlag = false;
for (int i = 0; i < accountInfoList.size(); i++) {
for (int j = i+1; j < accountInfoList.size(); j++) {
// compare list.get(i) and list.get(j)
System.out.println("i "+accountInfoList.get(i).getPhoneNumber()+" "+"i+1 "+accountInfoList.get(i+1).getPhoneNumber());
if(accountInfoList.get(i).getPhoneNumber() == accountInfoList.get(i+1).getPhoneNumber()){
isSamePhoneNumberFlag = true;
}
}
}
return isSamePhoneNumberFlag;
}
}
Output:
[AccountInfo{accountName='Rohit Sharma', phoneNumber='123456'}, AccountInfo{accountName='Mayank Singh', phoneNumber='123456'}, AccountInfo{accountName='sam Singh', phoneNumber='12378456'}, AccountInfo{accountName='Rahul Sharma', phoneNumber='123456'}]
i 123456 i+1 123456
i 123456 i+1 123456
i 123456 i+1 123456
i 123456 i+1 12378456
i 123456 i+1 12378456
i 12378456 i+1 123456
All Phone numbers are same
Upvotes: 0
Views: 38
Reputation: 1618
Try something like this, read the comment next to return statement :
public static boolean isSamePhoneNumber(List<AccountInfo> accountInfoList) {
for (int i = 0; i < accountInfoList.size(); i++) {
for (int j = i + 1; j < accountInfoList.size(); j++) {
// compare list.get(i) and list.get(j)
System.out.println("i " + accountInfoList.get(i).getPhoneNumber() + " " + "i+1 " + accountInfoList.get(i + 1).getPhoneNumber());
if (accountInfoList.get(i).getPhoneNumber() != accountInfoList.get(i + 1).getPhoneNumber()) {
return false; // just this line tells you that numbers are different. So no need to check further.
}
}
}
return true;
}
Upvotes: 1