0

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.");
     }

8 Answers 8

3

You can check if it contains it:

if(girls.contains(nameSearch)) {
   // Look for the data
} else {
   // print error
}
Sign up to request clarification or add additional context in comments.

Comments

0

Use a boolean to determine if you went into the condition, ie:

  if(nameSearch.equals(boys.get(i).getName()))
  {
       myBoolean = true;
       //...
  }

Then use:

 if(!myBoolean)
      System.out.println(nameSearch + " is not in the top 1000 boys names.");

Comments

0

You should keep a variable to check if you have found the name or not... similar to below.

nameSearch = keyboard.next();

boolean didFindMatch=false;
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.");
        didFindMatch=true;
        break;
  }

}


if (!didFindMatch)
  System.out.println(nameSearch + " is not in the top 1000 girl names.");

1 Comment

You will break after it has decided it's not in the list of girls. He also wants to search trough the list of boys.
0

To quickly search your List, you can use Collections.binarySearch(...). (see here). To use binarySearch your Collection must be sorted. The same Comparator used for sort (see here) must be used by binarySearch

You can use binarySearch to find a specified name

2 Comments

How does this answer the question?
The OP is apparently not quite versed in the art of java, so i really doubt you helped him by avoiding a concrete answer!
0

not the cleanest but should work pretty well.

bolean inboys = false;
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.");
inboys = true;

  }
}

if(inboys == false)
{
  System.out.println(nameSearch + " is not in the top 1000 boys names.");
}

Comments

0

I think your conditions should something like these. . .

int rank = 0;
int amount = 0;

for(int i = 0; i < girls.size(); i++)
{
  if(nameSearch.equals(girls.get(i).getName()))
  {
    rank = i + 1;
    amount = girls.get(i).getAmount();
    break;
  }

}
if (rank > 0 && amount > 0) {
    System.out.println(nameSearch + " is ranked " + rank + " in popularity among girls with " + amount + " namings.");
} else {
    for(int i = 0; i < boys.size(); i++) {
       if(nameSearch.equals(boys.get(i).getName())) {
           rank = i + 1;
           amount = boys.get(i).getAmount();
           break;
       }
    }
    if (rank > 0 && amount > 0) {
        System.out.println(nameSearch + " is ranked " + rank + " in popularity among boys with " + amount + " namings.");
    } else {
        System.out.println(nameSearch + " is not in the top 1000 girl names.");
        System.out.println(nameSearch + " is not in the top 1000 boys names.");
    }
}

Comments

0

Seems to me you could use the arraylist.indexof method to get the index, and then if it's not -1, use the index to get the amount of times the name appears. For this to work you would need to create some sort of person object, and be able to compare it with another person an object by name.

Person p = new Person(namesearch);
int index = (girls.indexOf(p);
if(index != -1) {
   // Print info
} else {
   // print error
}

Comments

0

contains() method is the best you could do.

As Jeroen Vannevel mentioned,

First search the name in girls arraylist and then boys arraylist like:

if(girls.contains(nameSearch))
{
    //print your result
} else if(boys.contains(nameSearch))
{
   //print your result
} else
{
   //print not found.
}

You don't have to loop through the whole arraylist.

Java has two types of contains method

1 - boolean java.lang.String.contains(CharSequence s)

2 - public boolean contains(Object o)

In your case it's a String object, choose 2nd type of contains() and it returns true if the String is found. You could get what you wanted.

Comments

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.