Results 1 to 8 of 8

Thread: hope to help me

  1. #1
    ABUALI is offline Member
    Join Date
    May 2010
    Posts
    4
    Rep Power
    0

    Question hope to help me

    am working on my retreival project and the project is to open a directory and take the text files from this directory and count each word occurrence am success to open the directory and build my program but i have aproplem opening the files
    i take all the text files into array and open it sequentially but the program say cannot open file and it is exist please help me and here is my code and by the way i need to print the result in a file
    PHP Code:
    package learn;
    
    import java.util.*;  // Provides TreeMap, Iterator, Scanner
    import java.io.*;    // Provides FileReader, FileNotFoundException
    
     public class retreival
             
     {
     public static void main(String[ ] args)
        {
     // **THIS CREATES A TREE MAP**
           TreeMap<String, Integer> frequencyData = new TreeMap<String, Integer>( );
    
           readWordFile(frequencyData);
           printAllCounts(frequencyData);
        }
    
    
     public static int getCount
        (String word, TreeMap<String, Integer> frequencyData)
     {
           if (frequencyData.containsKey(word))
           {  // The word has occurred before, so get its count from the map
        return frequencyData.get(word); // Auto-unboxed
           }
           else
           {  // No occurrences of this word
              return 0;
           }
        }
    
    
       public static void printAllCounts(TreeMap<String, Integer> frequencyData)
        {
           System.out.println("-----------------------------------------------");
           System.out.println("    Occurrences    Word");
    
           for(String word : frequencyData.keySet( ))
           {
              System.out.printf("%15d    %s\n", frequencyData.get(word), word);
           }
    
           System.out.println("-----------------------------------------------");
        }
    
    
        public static void readWordFile(TreeMap<String, Integer> frequencyData)
        {
           Scanner wordFile;
           String word;     // A word read from the file
           Integer count;   // The number of occurrences of the word
    
            String path;
            Scanner sc =new Scanner(System.in);
            System.out.println("enter your path plz");
            path=sc.nextLine();
           if (path.isEmpty()||path==null)
           {
            System.out.println("emty path" + "   " + path);
           }
           else
           {
                File dir=new File(path);
                String[] h=dir.list();
    
    
    
                for (int i=0; i<h.length; i++)
                {
                    String filename = h[i];
                }
                FilenameFilter filter = new FilenameFilter()
                { public boolean accept(File dir, String name)
                  { return name.endsWith(".txt");
                  }
                };
            
    
                h = dir.list(filter);
    
          
     //**FOR LOOP TO READ THE DOCUMENTS**
     for (int x=0; x<h.length; x++)
     { //start of for loop [*
     
           try
           {
              wordFile = new Scanner(new FileReader(h[x]));
           }
           catch (FileNotFoundException e)
           {
      System.err.println(e);
      return;
           }
    
           while (wordFile.hasNext( ))
           {
               // Read the next word and get rid of the end-of-line marker if needed:
       word = wordFile.next( );
    
       // This makes the Word lower case.
       word = word.toLowerCase();
    
        // Get the current count of this word, add one, and then store the new count:
             count = getCount(word, frequencyData) + 1;
             frequencyData.put(word, count);
           }
    
          } //End of for loop *]
        }
    
    
        }
    
    }
    hope to get quick reply
    thank u at all

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,620
    Rep Power
    23

    Default

    program say cannot open file
    Print out the name with delimiters (">" + filename +"<") and look at it closely to be sure there are no extra characters.

    What line in the program has the problem?

  3. #3
    ABUALI is offline Member
    Join Date
    May 2010
    Posts
    4
    Rep Power
    0

    Default

    sory for missunderstanding i mean that their is a problem with opening the files and here is the output with errors
    PHP Code:
    run:
    enter your path plz
    C:\h
    -----------------------------------------------
        Occurrences    Word
    java.io.FileNotFoundException: b.txt (The system cannot find the file specified)
    -----------------------------------------------
    BUILD SUCCESSFUL (total time: 2 seconds)
    and i check the names of files like u til me and here is the output
    PHP Code:
    run:
    enter your path plz
    C:\h
    >b.txt<
    >c.txt<
    BUILD SUCCESSFUL (total time: 6 seconds)

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,620
    Rep Power
    23

    Default

    What line in the program has the problem?
    So the dir.list() call returns 2 files and the FileReader() call fails? Does it fail on the first file or when?

  5. #5
    ABUALI is offline Member
    Join Date
    May 2010
    Posts
    4
    Rep Power
    0

    Default

    yea it is in the first file
    and i dont know in wich line the error occur

  6. #6
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    304
    Rep Power
    6

    Default

    Don't use list(), but rather listFiles() to obtain the Files in the directory. It probably has something to do with not using the entire path to the file, but rather just the file name.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,620
    Rep Power
    23

    Default

    What folder is the FileReader looking in? Is it the same folder that the dir.list() method got the list of files from?

  8. #8
    ABUALI is offline Member
    Join Date
    May 2010
    Posts
    4
    Rep Power
    0

    Default

    oh really thank u all using listFiles solve it really thank u all

Similar Threads

  1. hope there is an easier way.
    By IYIaster in forum New To Java
    Replies: 6
    Last Post: 10-14-2009, 07:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •