Hi, This code doesn't seem to compile. I can't seem to figure out what's wrong with it. The error I get is: cannot find symbol - method contains (java.lang.string)

 public void getName (String name) {

      for (olympian filename: Olympians) { 
      if (filename.contains(name)) {  
          System.out.println(filename);
        }

        else {

          System.out.println("Nothing found");  
        }

    } 

    }

Dani AI

Generated

The compiler message "cannot find symbol - method contains (java.lang.string)" happens because the loop variable (filename) is an olympian object and that class does not define a contains(String) method. contains is a method on String (for substring checks) and on collection classes (for membership). It is not a reserved word. As pointed out, either call contains on the olympian's name String or add an appropriate method to the olympian class.

Several problems in 's posted class make the search fail or behave incorrectly: the getters take parameters and return those parameters instead of the object's fields (e.g., getName(String name) should be getName() and return the private name field), one accessor is misspelled, and the class name should follow Java conventions (capitalized). Also, printing "Nothing found" inside the loop will print it for every non-matching element; it should be printed once after the loop if no match was found. An ArrayList typed ArrayList<Olympian> is correct for storing name+medal counts — primitive int values belong as fields inside the object, not as separate list elements.

A concise, correct approach (illustrates changes) — provide a getter and a name-contains helper, then search and print results only once:

public class Olympian {
    private String name;
    private int golds, silvers, bronzes;

    public Olympian(String name, int golds, int silvers, int bronzes) {
        this.name = name;
        this.golds = golds;
        this.silvers = silvers;
        this.bronzes = bronzes;
    }

    public String getName() { return name; }

    public boolean nameContains(String q) {
        return q != null && name != null && name.toLowerCase().contains(q.toLowerCase());
    }
}
public void printMatches(String search) {
    boolean found = false;
    for (Olympian o : olympians) {            // olympians is ArrayList<Olympian>
        if (o.nameContains(search)) {
            System.out.println(o.getName());
            found = true;
        }
    }
    if (!found) System.out.println("Nothing found");
}

Notes: use equalsIgnoreCase for exact-name matches, remember ArrayList.contains(...) only works when the list element type matches the argument (or when equals is implemented), and keep getters parameter-free so other code can access object state correctly.

Recommended Answers

All 13 Replies

Please post the full text of the error messages you get.

The error code is on line 4 on if (filename.contains(name)).

Please post the full text of the error message. The posted code won't compile so I can't get the full text of the error messaage from my compiler.

Here is a sample:

    TestSorts.java:138: cannot find symbol
    symbol  : variable var
    location: class TestSorts
             var = 2;
             ^

What are the definitions for: olympian and Olympians

I'm using BlueJ and this is the full error message I get: cannot find symbol - method contains (java.lang.string) olympian is an element and Olympians is an ArrayList.

Does the class of the object used as reference: filename have a contains(String) method defined in it?

cannot find symbol - method contains (java.lang.string)

Why is String spelled with a lowercase s? All java classes start with uppercase letters.

filename is just a local variable. olympian is the class.

Does it's class have a contains() method? The compiler can not find a definition for the contains() method in the olympain class. Look at the class's definition to see if there is one.

No, it doesn't this is all of the code:

/**
 * Write a description of class olympian here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class olympian
{
  private String name; 
  private int golds; 
  private int silvers;
  private int bronzes; 

  public olympian (String name, int golds, int silvers, int bronzes) {

      this.name = name; 
      this.golds = golds; 
      this.silvers = silvers; 
      this.bronzes = bronzes;


  }

  public String getName(String name){

      return name; 

    }

  public int getGolds (int golds) {

      return golds; 


  }   

  public int getBronzes (int bronzes) {


      return bronzes;

    }

  public int getSlivers (int silvers) {

   return silvers;    

 }

 public void changeName (String name) { 

  this.name = name;

} 

public void changeGold (int gold) {


  golds = gold; 

}

public void changeSivers (int Silvers ) {

 silvers = Silvers; 


}

public void changeBronze (int Bronzes) {

 bronzes = Bronzes;

}

}

No, it doesn't

That is exactly what the compiler is saying.

Why are you using a method that doesn't exist? Can you add the method to the class so the compiler can find it and not issue an error message?

Or you should define what the if test should do and rewrite it to do that.

Oh, I thought contains was a reserve word. So, how would I match a string in a ArrayList?

How is the ArrayList defined? Is it: ArrayList<String>
Then you should be able to use the ArrayList's contains() method to test if the ArrayList contains a String.

If the ArrayList does not contain Strings, then you can not use the ArrayList's contains() method to test for a String because the ArrayList does not contain String objects.

What is in the ArrayList?
Where is the String that you want to test for? If it is inside of an object, then the code has to use a method of that class to get at the String that the class contains.

In the ArrayList there is a name and an integer value. The string will be a parameter.

In the ArrayList there is a name and an integer value

That is not possible. An ArrayList can ONLY contain objects of one type. An int value is not an object.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.