-3

I want to compare from a void main method where there is a array and a string. so what I want to do is to compare with if name is in the array. return true else return false. But I cant get it to work. It gives me false on both. And I don't know why?

public boolean isActor(String name) {

        if(name.equals(actors)) {
            return true;

        }else {
            return false;
        }
    }

public static void main(String[] args) {
    String[] actors = {"Ulla Skoog", "Suzanne Reuter", "Peter Dalle"} ;
    Dvd d1 = new Dvd(10327, "Yrrol", "Peter Dalle", actors, 88);
    System.out.println("Medverkar Kalle Kula i Lorry: " + d1.isActor("Kalle kula"));
    System.out.println("Medverkar Ulla Skoog i Lorry: " + d1.isActor("Ulla Skoog"));

}

Any help would be appreciated !

EDIT: So I tried to do if(Arrays.asList(actors).contains(name)) and yes it worked. Problem is I might now be allowed to do it under a test which can make me losing points and I tried to make a for loop and by that make a equals statement but still getting the same result which is both false.

EDIT2: Also tried to do this

public boolean isActor(String name) {
          for(String s: actors){
                if(s.equals(actors))
                    return true;
            }
            return false;
        }

but still got the same result of both false

EDIT3:

What I want to do is to make e method which is ( public boolean isActor(String name) { ) and with that I want to make a if-statement which should make a algorithm by saying "If this name is on this array. make it say true. If its not in the array. Say its false." Thats what im trying to do.

Thrillofit123
  • 195
  • 1
  • 2
  • 12
  • Your code wouldn't even compile. My suggestion to atleast make it compile - and we are always there to help. There are lot of java tutorials on the net - https://docs.oracle.com/javase/tutorial/ is the official one from Oracle. – Dakshinamurthy Karra Aug 21 '15 at 11:33
  • The question linked above is how you do what you want to do. As to why your code is not working, `equals` does exactly what it says it does, it checks whether 2 things are equal, and a `String` is not equal to an array, regardless of what's actually in this array (just like I'm not equal to the room I'm currently in). – Bernhard Barker Aug 21 '15 at 11:36
  • Oh. So I tried to make a if(Arrays.asList(actors).contains(name)) and yes it worked. but there should be easier way to do it since I dont really know if its allowed to do this under test in school so I tried to make a for loop and then make a if(name.equals(actor) but it gives me the same result which Should work if im right? – Thrillofit123 Aug 21 '15 at 11:44
  • There's presumably something wrong with your for-loop then. [One of the answers in the linked post](http://stackoverflow.com/a/12635769/1711796) has a for-loop which you can use as a starting point ([here](http://stackoverflow.com/a/23526351/1711796)'s a simpler version). – Bernhard Barker Aug 21 '15 at 11:46
  • @Dukeling so I did the number 3 and it still gave me the same result. which will be for me public boolean isActor(String name) { for(String s: actors){ if(s.equals(actors)) return true; } return false; } – Thrillofit123 Aug 21 '15 at 11:51
  • @Thrillofit123 why you don't use `name` to test equalty ? – Hacketo Aug 21 '15 at 11:53
  • @Thrillofit123 What you are doing buddy ? First be clear about what you want to do and den only try for that. – SacJn Aug 21 '15 at 11:53
  • What I want to do is to make e method which is ( public boolean isActor(String name) { ) and with that I want to make a if-statement which should make a algorithm by saying "If this name is on this array. make it say true. If its not in the array. Say its false." Thats what im trying to do. – Thrillofit123 Aug 21 '15 at 11:56
  • @Thrillofit123 use convexHull answer, it does what you want – Hacketo Aug 21 '15 at 11:59
  • @Hacketo I have done it and it still gave me false on everything. Which I dont understand why. Logical it should work. I might see what could be wrong. I will try with debugg now. – Thrillofit123 Aug 21 '15 at 12:01
  • @Thrillofit123 Well your example of edit2 is not using `name` to do the test so it can't work, you should look the answer more carefuly – Hacketo Aug 21 '15 at 12:03

2 Answers2

2

You have to test each item in array:

public boolean isActor(String name) {

    for (String actor : actors) {
        if (name.equals(actor) {
            return true;
        }
    }
    return false;
}
convexHull
  • 1,761
  • 2
  • 15
  • 18
1
public boolean isActor(String name, List<String> actorList) {

    if(actorList.contains(name)) {
        return true;

    }else {
        return false;
    }
}

public static void main(String[] args) {
    String[] actors = {"Ulla Skoog", "Suzanne Reuter", "Peter Dalle"} ;   
    List<String> actorList = Arrays.asList ( actors);
    Dvd d1 = new Dvd(10327, "Yrrol", "Peter Dalle", actors, 88);
    System.out.println("Medverkar Kalle Kula i Lorry: " + d1.isActor("Kalle kula",actors));
    System.out.println("Medverkar Ulla Skoog i Lorry: " + d1.isActor("Ulla Skoog",actors));
}
SacJn
  • 777
  • 1
  • 6
  • 16
  • Problem is I cant take the String[] actors up to the method because it is not allowed for me do to it. So is there any other way? – Thrillofit123 Aug 21 '15 at 11:44
  • pass that also to the method – SacJn Aug 21 '15 at 11:46
  • Sorry to say but I dont think I have to make a add on parameters. I also think im not allowed to add something. So yeah. I should start with public boolean isActor(String name) { and make a statement take will it true or false by static void main method. – Thrillofit123 Aug 21 '15 at 11:54