Josh
Josh

Reputation: 3833

Java: Comparing ArrayList item to User Input String

I have seen other threads on this, but I still don't understand the best approach. Issue is as started: One can't compare user input that is a String to an arrayList item (object). My List will be over 40 items.

So when I try to compare classesList.get(0) to the user input mage. It won't work.

List<String> classesList = new ArrayList<String>();
classesList.add("mage");
classesList.add("warrior");
classesList.add("thief"); 

Scanner input = new Scanner(System.in);

String input = input.next();

for (int counter = 0; counter < classesList.size(); counter++) { 
    if (input == classesList.get(counter)) {
        //won't run body since can't compare
    }
}

I found some who converted the arrayList to be a String. But I am unsure how I could then easily search through my list. I'd much rather leave it as a List instead. Is the best method to this approach?

String[] classesArray = classesList.toArray(new String[classesList.size()]);

Upvotes: 2

Views: 29968

Answers (4)

Vallabh Patade
Vallabh Patade

Reputation: 5110

I think you have to see if input string exist in the ArrayList, you have to use contains() method.

   if(classesList.contains(input)) {
       //Found in the arraylist.
   }

But with you method, you can't compare strings using == operator. You should use equals() method for comparision

List<String> classesList = new ArrayList<String>();
classesList.add("mage");
classesList.add("warrior");
classesList.add("thief"); 

Scanner input = new Scanner(System.in);

String input = input.next();
boolean isExist = false;
for (String item : classesList) { 
    if (input.equals(classesList.get(counter))) {
        //Your logic if it's there.
         isExist = true;
         break;
   }
}

 if(isExist) {
     //Found in the arraylist.
 }

Also if you want to make this comparision with ingnoring the case, you shoudl consider using equlasIgnoreCase() method instead of equals() method.

Upvotes: 3

Makoto
Makoto

Reputation: 106498

You can use the contains method on your list to see if a particular string is contained inside of it, instead of iterating across it. However, the underlying list will still be iterating over the contents of the list internally.

What you could do instead, to improve speed, is to use a Set instead, which has a nice property of having contains be run in constant time.

Set<String> classesSet = new HashSet<>();
// none of your other code has to change, except for:

if(classesSet.contains(input)) {
    // logic
}

Upvotes: 2

Sachin Thapa
Sachin Thapa

Reputation: 3719

Yes what you read is right, not to use following:

for (int counter = 0; counter < classesList.size(); counter++) { 
    if (input == classesList.get(counter)) {
        //won't run body since can't compare
    }
}

You cannot use == for comparison instead you should use equals method if you are using loop:

for (int counter = 0; counter < classesList.size(); counter++) { 
    if (input.equals(classesList.get(counter))) {
        //won't run body since can't compare
    }
}

Otherwise easier way is to use contains as mentioned in other answer.

Also see How do I compare strings in Java?

Cheers !!

Upvotes: 2

Adam
Adam

Reputation: 36743

I don't fully understand your question, however you can test if a given String exists in the list using contains()

Some other pointers

  • Never compare Strings with ==, use .equals(). See this question
  • Iteration can be done more simply using (String element : list) syntax

Upvotes: 2

Related Questions