7 Replies Latest reply: Aug 13, 2007 3:07 AM by 807600 RSS

    PrintWriter Fails to Print

    807600
      Hello,
      Please can someone tell me why the PrintWriter fails to print.
      public class Frequency{
      private PrintWriter dMR;
      -
      -
      -
      -
      
      public void DM(List <String []> store) {
               open_dMR();
               List <Integer> mapList;
               int countElem = 1;
               
               for(int x = 0; x < store.size(); x++ ) {
                    for(int y = 0; y < store.get(x).length; y++) 
                         for(int k = x+1; k < store.size(); k++) {
                              for(int m = 0; m < store.get(k).length; m++) 
                                   if(store.get(x)[y].compareTo(store.get(k)[m]) == 0 ) {
                                        countElem++;
                                        
                                        if(map.get(store.get(x)[y])>= thres && ((k-x) >= thres)) {
                                             mapList =  new ArrayList <Integer>();
                                             if(!dMap.containsKey(store.get(x)[y])) {
                                                  mapList.add(x + 1);
                                                  mapList.add(k + 1);
                                                  dMap.put(store.get(x)[y], mapList);
                                             }
                                             else 
                                             {
                                                  mapList = dMap.get(store.get(x)[y]);
                                                  mapList.add(x + 1);
                                                  mapList.add(k + 1);
                                                  dMap.put(store.get(x)[y], mapList);
                                                                                         }
                                             dMR.println(store.get(x)[y]);
                                                              //debug
                                             System.out.println(store.get(x)[y]);
                                             }
                                        }
                                     }
                                       countElem = 1;
                                 }
                                  close_dMR();
                         }
      
       
      private void open_dMR() {
                try {
                     dMR = new PrintWriter("dMR.txt");
                    } 
                      catch (FileNotFoundException e) 
                         {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
                   }
               }
      
      
      private void close_dMR() 
          {
               dMR.close();
          }
      
      
      }
      When I run the program, it creates an empty text file. I tried to debug by placing the PrintWriter open and closing method above and below where it prints, e.g:
       
      open_dMR();
      dMR.println(store.get(x)[y]);
      close_dmR();
      and it printed just the last element which is logical. If I place it at outmost loop, it prints nothing. The "System.out" works fine and prints all the loop results.

      Thanks,

      Jona_T
        • 1. Re: PrintWriter Fails to Print
          807600
          I'm sorry, I saw your 11-bajillionty-thousandth nested for statement, started to convulse, shat my pants and started mumbling obscenities. Luckily, my wife saw what was going on and hit the "back" button on my browser. I'm feeling much better now.
          • 2. Re: PrintWriter Fails to Print
            807600
            Jona,

            Please at the very least put { and } around ALL of your for loops. With all the nesting it's hard enough to count with some of the fors not having {
            and } it's next to impossible.

            I suspect a simple count of braces would show you your problem. I think your close_dmr() is in one of your loops and is getting closed the
            first time through before you've wriiten anything. I think.

            Another thing... I know we talked about it before. The depth of for loop nesting is bananas. You're going to continue to have bugs like this and
            it makes the code very hard to follow.

            As a suggestion: recursion. I think there must be some ways you can break this apart into several methods or something...
            • 3. Re: PrintWriter Fails to Print
              807600
              Hello Cotton.m
              I put { and } in all the loops and it still did not work. I will try and shorten the code by using recursion to see if it works.

              Thanks.
              • 4. Re: PrintWriter Fails to Print
                807600
                Just to confirm...

                is everything printing properly to the console (aka System.out) ?
                • 5. Re: PrintWriter Fails to Print
                  807600
                  Yes, everything prints properly in the console, that is the reason why am' a bit confused.
                  • 6. Re: PrintWriter Fails to Print
                    807600
                    Hello,

                    I have seen the problem. It is because I implemented "runnable" in my program. So, in each run, the Printwriter opens the same text file and writes the new result thereby overwriting the old result. In the end, I will only have the last result. Is there a way I can tell the Printwriter not to overwrite the old results?

                    Thanks
                    • 7. Re: PrintWriter Fails to Print
                      807600
                      Hello,
                      I got it right now. Thanks