Can you help me with this code, I do not know what is wrong? Thread is stuck somewhere?

       import java.io.*;
   import java.util.*;
   import java.util.concurrent.locks.ReentrantLock;


   public class Pokemon
   {

      private static ArrayList<String> pokemonList = new ArrayList<String>();
      private static ArrayList<Thread> threads = new ArrayList<Thread>();
      private static ReentrantLock lock = new ReentrantLock();

      public Pokemon()
      {


         int k =1;

         while(true)
         {


            String file = "PokeRecord_"+k+".dat";

            File newFile = new File(file);

            if(newFile.exists())
            {
               try{

                  Thread runLab = new Thread(new PlayMe(newFile));

                  runLab.start();
                        threads.add(runLab);
                  Thread.sleep(50);
               }

                  catch (InterruptedException ioe){}

            }
            else 
            {
               //System.exit(0);
                    //false;
               break;


            }

            k++;
         }

         for (int i = 0; i < threads.size(); i++) {
            try {
               threads.get(i).join();
            } 
               catch (InterruptedException ignore) {}
         }

         System.out.printf("Loaded list size has %,8d Pokemnos.%n", pokemonList.size());


      }


      public class PlayMe implements Runnable
      {
         private File newFile;
         private String name;
            private long sightings;


         public PlayMe(File newFile)
         {
            this.newFile = newFile;

         }

         public void run()

         {

            try{

               FileInputStream fis = new FileInputStream(newFile);
               DataInputStream dis = new DataInputStream(fis);

               while (true)

               {
                  try{
                     name = dis.readUTF();
                     sightings = dis.readLong();

                  //System.out.println(name);   

                     lock.lock();

                     pokemonList.add(name);

                     lock.unlock();



                  }
                     catch (IOException e3of){ //System.err.println("End of File" + eof);
                     }

               }


            }
               catch (FileNotFoundException fnf){ //System.err.println("End of File" + eof);
               }


         }


      }



      public static void main(String[] args) {
         new Pokemon();

         if(args.length > 0)
         {


            for (String a: args)
            {

               int sight = pokemonList.indexOf(a);


            // not found
               if(sight == -1)
               {
                  System.out.printf("\n%-17s NO POKEMON", a);
                  System.exit(0);
               }    
                // found


               else
               {
                  System.out.printf("\n%-17s was seen %8s times", a, sight);
                  System.exit(0);
               }



               System.out.println(" was NOT found");

            }
         }
         else 
         {
            System.exit(0);
         }

      }


   }

Thread is stuck somewhere?

To find where the code is executing, add lots of println statements to your code that will record where the program is executing. The print out will show you what the code is doing.

, I am doing something wrong

Please explain.
If you got an error message you need to post it if you want help with it.

What does "failed" mean?
Did you add the printlns to show the execution flow?
You need to do that to debug your code and find out what it is doing.

Error is here I think because When I take this out it works somehow?

   for (int i = 0; i < threads.size(); i++) {
        try {
           threads.get(i).join();


        } 
           catch (InterruptedException ignore) {}
     }

Yes the join() method would hold up execution until the thread dies.
Did you add println statements to show when threads where ending? Which threads ended and which were still executing?

If I put print just before this if i get

[Thread[Thread-1,5,main], Thread[Thread-2,5,main], Thread[Thread-3,5,main], Thread[Thread-4,5,main]]

I do not get it?

What are you printing? Do you print out any message when a thread ends?

You are calling join(). When do any of the threads end?

It does not end that is the problem it halts, I input 5 files and it does nothing just halts?

But if I remove that IF with join it ends and prints normaly?
It comes to line 53 and it hals, it does not do anthing, that is the problem...

Edited 4 Years Ago by mehnihma: w

if I remove that IF with join

What IF?

it does not do anthing

It is waiting for the thread to end.
The join will not return until the thread ends. When does the thread end?????

if I remove that IF with join

What IF?

it does not do anthing

It is waiting for the thread to end.
The join will not return until the thread ends. When does the thread end?????

I got it, my try catch was the problem, it catched IOException not the EOF

I thinks this was the problem

   while (true)

           {

                 name = dis.readUTF();
                 sightings = dis.readLong();

              //System.out.println(name);   

                 lock.lock();

                 pokemonList.add(name);

                 lock.unlock();



              }

           }





           catch (FileNotFoundException fnf){ 
           }

                catch (IOException e3of){ 

                 }



     }

Now i have another problem :D
When I enter arguments it wont find me string in the array list?

for (String a: args)
        {

           int sight = pokemonList.indexOf(a);


        // not found
           if(sight == -1)
           {
              System.out.printf("\n%-17s NO POKEMON", a);
                    //System.out.println(pokemonList);
              //System.exit(0);
           }    
            // found


           else
           {
              System.out.printf("\n%-17s was seen %8s times", a, sight);
              System.exit(0);
           }

User inputs pokemon names and program needs to find the name and the number of sightings of that Pokemon,
I get error that pokemon was not found, also I do not know how to enter number of sightings to and from the array here?
Can you help me with that?

In this code I have just added pokemon names from the files to array, so now I need to add number of sightings also and find them with arguments
Do I need to add sightings in the same way or? and if yes how to get them both out?

      while (true)

           {

                 name = dis.readUTF();
                 sightings = dis.readLong();

              //System.out.println(name);   

                 lock.lock();

                 pokemonList.add(name);

                 lock.unlock();



              }

Edited 4 Years Ago by mehnihma: adding

If you want to associate a long value with a String, look at using a Map.
The key would be the String and the value would be the Float value.

I did it like this? But my main for is wrong I think because it does not work?

 pokemonList.add(name);
 pokemonList.add(String.valueOf(sightings));
This question has already been answered. Start a new discussion instead.