1

I'm getting this error when trying to compile a class using the contains method in java. The error says that it cannot find the symbol, which I find a bit weird considering I've used the contains method several times before without any issues.

The error I'm getting looks like this:

https://i.imgur.com/tD1ty3A.png

Here's my code:

import java.util.*;

public class DVDAdministrasjon {

private Map personerOversikt;

public void nyPerson(String navn){
  for(int t = 0; t < personerOversikt; t++){
  if(!personerOversikt.get(t).equals(navn)){
    Person newPerson = new Person(navn);
    personerOversikt.put(navn, newPerson);
  } else {
    System.out.println("Personen er allerede registrert!");
  }
  }
}
public void kjop(Map kjoptDVD, Map eier){
  int teller = 0;
  Scanner tastatur = new Scanner(System.in);
  System.out.println("Hvem har kjopt DVD-en?");
  String navn = tastatur.nextLine();
  System.out.println("Hva er tittelen paa DVD-en?");
  String filmNavn = tastatur.nextLine();
  if(personerOversikt.contains(navn)){
    kjoptDVD.put(navn, filmNavn);
    eier.put(navn, filmNavn);
  } else {
    System.out.println("Personen er ikke registrert enda!");
  }
  }
  public void laan (Map utlaantDVD, Map eier, Map laantDVD){
    Scanner tastatur = new Scanner (System.in);
    System.out.println("Hvem vil laane DVD-en?");
    String navn = tastatur.nextLine();
    System.out.println("Hvem skal DVD-en laanes fra?");
    String navn2 = tastatur.nextLine();
    System.out.println("Hva er tittelen paa DVD-en?");
    String filmNavn = tastatur.nextLine();
    if(eier.contains(filmNavn)){
      if(personerOversikt.contains(navn2)){
      laantDVD.put(navn, filmNavn);
      utlaantDVD.put(navn2, filmNavn);
    } else{
      System.out.println(navn2 + " er ikke registrert");
    }
  } else{
    System.out.println(navn2 + " eier ikke filmen");
  }
  }
}

Here's the referenced class:

import java.util.*;

public class Person {

private String navn;
private Map kjoptDVD;
private Map utlaantDVD;
private Map laantDVD;
private Map dvdOversikt;

public Person (String navn){
 this.navn = navn;
 }
}
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
Knut Eriksen
  • 27
  • 1
  • 8

3 Answers3

1

The reason of the error is quite easy: Map interface doesn't have a method named contains

you need to use either Map.containsKey or Map.containsValue but contains as it is is not defined and is not going to work..

Additionally, Map is an interface so maybe the class you are implementing is a HAshMap.. so containsKey and ContainsValue are methods using internally equals and hascode just in case you have a custom class in there like PErson class or similar..

and don't use raw types with the Collection...

snippet containsValue in the HashMap class...

public boolean containsValue(Object value) {
    Node<K,V>[] tab; V v;
    if ((tab = table) != null && size > 0) {
        for (int i = 0; i < tab.length; ++i) {
            for (Node<K,V> e = tab[i]; e != null; e = e.next) {
                if ((v = e.value) == value ||
                    (value != null && value.equals(v)))
                    return true;
            }
        }
    }
    return false;
}
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0

You are using maps instead of collections. If you check Map javadocs, you'll see that the Map interface doesn't have a contains method. You should use containsKey instead.

fps
  • 33,623
  • 8
  • 55
  • 110
0

Map does not have a method contains. Try containsKey.