Hello so I have two arraylists in my problem, one filled with the top 1000 girl names and the amount of times they're used and one filled with the top 1000 boy names and the amount of times they're used. The user inputs a name, searches through both arraylists and outputs if the name is in the top boy or girl names, what their ranking is and how many times they're used. If the name is in the top girls it also needs to output The name is not a part of the top 1000 boy names and the same if it was a boy name. This is what I have
public void search()
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter the name you would like to search.");
nameSearch = keyboard.next();
for(int i = 0; i < girls.size(); i++)
{
if(nameSearch.equals(girls.get(i).getName()))
{
System.out.println(nameSearch + " is ranked " + (i+1) + " in popularity among girls with " + girls.get(i).getAmount() + " namings.");
}
}
else
System.out.println(nameSearch + " is not in the top 1000 girl names.");
for(int i = 0; i < boys.size(); i++)
{
if(nameSearch.equals(boys.get(i).getName()))
{
System.out.println(nameSearch + " is ranked " + (i+1) + " in popularity among boys with " + boys.get(i).getAmount() + " namings.");
}
}
else
System.out.println(nameSearch + " is not in the top 1000 boys names.");
}
Obviously I get an error because i can't have an else statement without an if before it.. Everything works as far as searching the array and finding the rank and whatever i just need it to output the right stuff. I am not sure at all what to do to get this to work.
update- i tried the contains method and i can never get it to work. it always outputs that it can not find the name in either arrays when i know it is a name that is there. Let me know if this looks wrong because it won't work correctly
Thanks! --Jess
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter the name you would like to search.");
nameSearch = keyboard.next();
if(girls.contains(nameSearch))
{
for(int i = 0; i < girls.size(); i++)
{
if(nameSearch.equals(girls.get(i).getName()))
{
System.out.println(nameSearch + " is ranked " + (i+1) + " in popularity among girls with " + girls.get(i).getAmount() + " namings.");
}
}
}
else
System.out.println(nameSearch + " is not in the top 1000 girl names.");
if(boys.contains(nameSearch))
{
for(int i = 0; i < boys.size(); i++)
{
if(nameSearch.equals(boys.get(i).getName()))
{
System.out.println(nameSearch + " is ranked " + (i+1) + " in popularity among boys with " + boys.get(i).getAmount() + " namings.");
}
}
} else
{
System.out.println(nameSearch + " is not in the top 1000 boys names.");
}