0

In my program I have an array with team names and what I want to do is collect user input that checks if the input matches any of the team names in the array. I can only get it to check one string in the array at a time if i put in the arguement of the if statement:

if(teamName.equals(teams[0])

. But I want to check all the strings in the array rather than one at a time

    Scanner input = new Scanner(System.in);

    String[] teams = new String [20];
    teams[0] = "Arsenal";
    teams[1] = "Aston Villa";
    teams[2] = "Burnley";
    teams[3] = "Chelsea";
    teams[4] = "Crystal Palace";
    teams[5] = "Everton";
    teams[6] = "Hull City";
    teams[7] = "Leicester City";
    teams[8] = "Liverpool";
    teams[9] = "Manchester City";
    teams[10] = "Manchester United";
    teams[11] = "Newcastle United";
    teams[12] = "QPR";
    teams[13] = "Southampton";
    teams[14] = "Sunderland";
    teams[15] = "Spurs";
    teams[16] = "Stoke";
    teams[17] = "Swansea";
    teams[18] = "West Ham";
    teams[19] = "West Brom";

System.out.println("Please enter a team: ");
    String teamName = input.nextLine();

    if(teamName.equals(teams)) {
            System.out.println("You like: " + teamName);
    }
    else {
        System.out.println("Who?");
    }
}   
  • Try taking a look at [this](http://stackoverflow.com/questions/1128723/in-java-how-can-i-test-if-an-array-contains-a-certain-value) link. – user2258651 Mar 23 '15 at 01:38

3 Answers3

2

Using java8, this would be a possible solution:

 if(Arrays.stream(teams).anyMatch(t -> t.equals(teamName))) {
     System.out.println("You like: " + teamName);
 } else {
     System.out.println("Who?");
 }
fabian
  • 80,457
  • 12
  • 86
  • 114
0

Just put them in a Set and use the contains method.

So make the following changes:

Set<String> teamSet = new TreeSet<>();
Collections.addAll(teamSet, teams);

System.out.println("Please enter a team: ");
String teamName = input.nextLine();

if (teamSet.contains(teamName)) {
    System.out.println("You like: " + teamName);
} else {
    System.out.println("Who?");
}
Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
  • You could of course also put the teams individually in the `Set` but that would be tedious :) – Maarten Bodewes Mar 23 '15 at 00:46
  • Thanks for the help! I was also wondering if you knew a way to check input of team names without the first letter having to capitalized for it to be accepted in the "if"? – Andrew Quinonez Mar 23 '15 at 01:04
  • The easiest method of doing this is to use e.g. lowercase for everything. The advantage of Paul's asnwer is that you could use `equalsIgnoreCase` which is not (directly) possible for the `Set` approach. You could think of `anyMatch` as a loop though, I don't assume it uses `hashCode` internally. – Maarten Bodewes Mar 23 '15 at 01:15
0

add this method to your code

public boolean arrayContainsTeam(String team)
{
    boolean hasTeam = false;
    for(String aTeam:teams) {
         if(aTeam.equals(team)) {
              return(true);
         }
    }
    return(false);
}

and then replace

if(teamName.equals(teams)) {
        System.out.println("You like: " + teamName);
}
else {
    System.out.println("Who?");
}

with

if(arrayContainsTeam(teamName)) {
        System.out.println("You like: " + teamName);
}
else {
    System.out.println("Who?");
}
Henry H
  • 11
  • 1