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

    } 

    }

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.