0

This is one of my method in my program. When I tried to search for the course code from one of the selection of characters: j, g, or t; it only prints the first line it finds and never searches for the next course code.

NKDSG Foo 12.1 5 T

YDSSE Kim 12.1 5 J

ESATF Lar 23.5 10 J

   private Dogs searchForDogsByCourse(String dogsCode)
    {

        System.out.printf("%27s%27s%27s%27s%n","ID","Name","Running Time","Penalty");
        int i = 0;
        for(Dogs dogs : dogsList)
        {
            i++;
            if(dogs.getCourseCode().equalsIgnoreCase(dogsCode))
            {   
                System.out.printf("%27s%27s%27.2f%27d",dogs.getDogId(), 
dogs.getName(), dogs.getDogRuntime(), dogs.getPenalty());

            }
        }
        return null;
    }

When I searched for J, it only prints first one it matches and ends the loop.

ID Name Running Time Penalty

YDSSE Kim 12.1 5

Nico Robin
  • 91
  • 7
  • What do you expect `return dogs;` to do? – Tunaki Jan 29 '17 at 22:55
  • @Tunaki oh, I that shouldn't be there. I forgot to delete it. – Nico Robin Jan 29 '17 at 22:59
  • your problem is with printing ? – Mohsen_Fatemi Jan 29 '17 at 23:00
  • The answer [that solved the problem](http://stackoverflow.com/questions/41926885/using-enhanced-for-loop-to-search-for-a-specific-character-but-only-prints-the#comment71034536_41926934) is exactly about that part of the code, so obviously it was still in the code... The duplicate explains it. – Tunaki Jan 29 '17 at 23:08
  • @Tunaki Thanks :)! I've been working for this program for hours; I just ignore everything and I was concentrating more on the results in my program. – Nico Robin Jan 29 '17 at 23:38

1 Answers1

1

The return statement in the if brings you out of the method.you should remove this statement to allow the for loop to continue. You Can put all the correct results in an array, and return this array at the end of the method

Greg Artisi
  • 172
  • 2
  • 12
  • Thank you so much man! This was the only part of my code that didn't work and you saved me from sitting here for another an hour. Thanks! – Nico Robin Jan 29 '17 at 23:06