2

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()]);
Josh
  • 3,673
  • 3
  • 20
  • 27

4 Answers4

3

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.

Vallabh Patade
  • 4,960
  • 6
  • 31
  • 40
  • This is perfect. Say I wanted to check if the user inputed "mage" and I wanted to load a few other methods. What would be the best approach for this checker? – Josh Aug 01 '14 at 21:08
  • @tdm Didn't get you. Can you please explain a little bit? – Vallabh Patade Aug 01 '14 at 21:11
  • Something like `if (input.equals(classesList.get(0)) == "mage") {//do stuff}` being able to specifically do something if the user inputs a specific string from my List. Make sense? – Josh Aug 01 '14 at 21:35
  • Nm, I figured it out. Thanks a ton for the help again. :) – Josh Aug 01 '14 at 22:03
2

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
Community
  • 1
  • 1
Adam
  • 35,919
  • 9
  • 100
  • 137
2

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 !!

Community
  • 1
  • 1
Sachin Thapa
  • 3,559
  • 4
  • 24
  • 42
  • Possibly a dumb question... but asking anyhow, why is this the case with: `==` tests for reference equality. `.equals()` tests for value equality. Is it the because the reference(?) number in the memory different? Since visually we see "foo" and "foo" as the same, yet they would not be for case: `new String("test") == new String("test") // --> false` – Josh Aug 01 '14 at 21:05
2

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
}
Makoto
  • 104,088
  • 27
  • 192
  • 230