Skip to content
Advertisement

how to compare the two names in array list

I am trying to compare the two names in the baby array list if it is true it will return “Two babies in the array are the same” if false will return “No same baby in the array”. But I just can’t get it to work for the findTwoSameBabies method, it is supposed to compare all the names in the array and return the result.

import java.util.ArrayList;
import java.util.Scanner;

public class BabyClient {

private static Baby[] babies;

public static void main(String[] args) {
    // Initialize ArrayList of Baby
    ArrayList<Baby> babiesList = new ArrayList<>();
    // Object of Scanner class to take input
    Scanner input = new Scanner(System.in);
    // Populating babies array list
    for (int i = 0; i < 4; i++) {
    // a.
        getBabyDetails(babiesList, input);
    }
    // b.
    printBabyDetails(babiesList);
    
    // c.
    System.out.println("nThe average age of all babies in the array is " + calculateAndPrintBabiesAverageAge(babiesList));
    
    // d.
    findTwoSameBabies(babiesList);

    
}

private static void getBabyDetails(ArrayList<Baby> babies, Scanner input) {
    // get name and age of the baby
System.out.print("Enter name of baby: ");
String name = input.next();
System.out.print("Enter age of baby: ");
int age = input.nextInt();
input.nextLine();
// create an baby instance
Baby baby = new Baby(name, age);
// add baby to the list
babies.add(baby);

}

private static void findTwoSameBabies(ArrayList<Baby> babiesList) {
    boolean sameBaby = false;
    for (Baby babie : babiesList) {
        for(int i=0;i<babiesList.size();i++) {
            for(int j=i+1;j<babiesList.size();j++) {
                if(babie.getName().equalsIgnoreCase(babiesList.size())) {
                    sameBaby = true;
                    break;//break from inner for loop j
                }
            }
            if(sameBaby) {
                break;//break from outer for loop i
            }
        }
    }
    
    if(sameBaby) {
        System.out.println("Two babies in the array are the same");
    }else {
        System.out.println("No same baby in the array");
    }
}


private static double calculateAndPrintBabiesAverageAge(ArrayList<Baby> babies) {
    int totalAge = 0;
    for (Baby babie : babies) {
        totalAge += babie.getAge();
    }
    double averageAge = totalAge / babies.size();
    return averageAge;
    }

private static void printBabyDetails(ArrayList<Baby> babiesList) {
    for(int i=0;i<babiesList.size();i++) {
        System.out.println("Baby "+(i+1)+ ": " + babiesList.get(i).getName() + ", " + "Age: " + babiesList.get(i).getAge());
        System.out.println();
        
    }
}

}

Advertisement

Answer

There are many problems with your code.

  • You have a most outer loop that iterates over the list, this is fine
  • You have a loop inside this loop that iterates over with an index
  • And an another, similar loop that goes ahead with one element from this element
  • But then you compare a name of the baby you get first with the size of the array. This is a number, and if your baby is not called as like 42 then you always get false in this case.

I honestly do not know what’s your real job to do here. I assume this is a homework and you have to prove your loop programming skills. Given this assumption, I recommend you the following solution:

private static void findTwoSameBabies(ArrayList<Baby> babiesList) {
    boolean sameBaby = false;
    for (Baby baby1 : babiesList) {
        for(int i=0;i<babiesList.size();i++) {
            Baby baby2 = babiesList.get(i);

            if(baby1.getName().equalsIgnoreCase(baby2.getName())) {
                sameBaby = true;
                break;//break from inner for loop i
            }           
        }

        if(sameBaby) { 
            break; // Break from the outer foreach loop 
        }
    }
    
    if(sameBaby) {
        System.out.println("Two babies in the array are the same");
    } else {
        System.out.println("No same baby in the array");
    }
}
  • We iterate over the list elements in the outer loop
  • And iterate over the same list in the inner loop
  • We compare every baby with every other babies to check duplicates
  • And break both loops if we found same names
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement