-1

Here is my method....

 void getMerchandise(String category){
         for(int i=0; i<merchandise.size(); i++){
             if(merchandise.get(i).getCategory()== category)
                 System.out.println("\n"+merchandise.get(i).toString()+ "\n");
         }

Okay, now if I call the method using...

  park1.getMerchandise("Hats");

It works just fine and prints all the items with Hats as the category.

However if I do....

     String z= input.next();
     park1.getMerchandise(z);

and then I type in the word Hats, it gives me nothing. I have another method in which I do the exact same thing, and it works fine. I have the scanner imported, and the scanner is static and is called input. So there are no errors there.

Nathan777
  • 79
  • 8

1 Answers1

1

Use equals to compare strings.

if(merchandise.get(i).getCategory().equals(category))

SHG
  • 2,516
  • 1
  • 14
  • 20
  • That's weird. It does work now. I don't understand why though. It was still working before when I had it my way when I didn't put any input in. – Nathan777 May 01 '17 at 01:59
  • @Nathan777 you'll need to know [the difference between “.equals” and “==”](http://stackoverflow.com/questions/1643067/whats-the-difference-between-equals-and) – Ousmane D. May 01 '17 at 02:05
  • I tried the .equals in another method instead of the ==, and it didn't work, but it was for a number of type long. – Nathan777 May 01 '17 at 02:06