mardi 26 juillet 2016

Java if statement in nested loop not matching , counting first occurrence [duplicate]

This question already has an answer here:

I have been having an issue which I feel should be simple to do but I cannot understand why it is not returning the expected results.

I have an array of Strings called User and a List of objects called Sales. Within Sales there is an field called User. What I wish to do is for each User in the array, I wish to match the sales object.

String[] User = {"John","Bob","Pete"};

   //The sales object looks like 

   {
   User: Bob
   Dep: Hardware 
   }

   {
   User: Bob
   Dep: Software 
   }
...

//The nested loop looks like

  for (int i = 0; i < User.length; i++)
        {   
        for (int j = 0; j < Sales.size(); j++ )
        {
                if (User[i] == Sales.get(j).getUser())
                {
                       System.out.println(Sales.get(j).getUser() +  Sales.get(j).getDep()   
            }
            }

What I am expecting to printed is:

Bob Hardware

Bob Software

However all I am seeing is

Bob Hardware

Can anyone see what is wrong with my logic/approach?

Aucun commentaire:

Enregistrer un commentaire