word/line/character count in files

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jul 2008
Posts: 28
Reputation: newtechie is an unknown quantity at this point 
Solved Threads: 0
newtechie newtechie is offline Offline
Light Poster

word/line/character count in files

 
0
  #1
Aug 30th, 2008
i did a codig to find the no. of words,no. of characters and no. of lines in a file i was not getting the output, can anyone help me

import java.io.*;
public class countCharacters  
{
    public  void Lines()throws IOException
    {
        File f = new File("in.dat");
        FileReader fr = new FileReader(f);
        BufferedReader br = new BufferedReader(fr);
        String str = br.readLine();
                        
            LineNumberReader ln = new LineNumberReader(br);
            int count = 0;
            while (ln.readLine()!=null)
            {
                count++;
            }
            
            System.out.println("no. of lines in the file = " + count);
         
        }
         public  void Words()throws IOException
            // count no. of words.
         {
             File f = new File("in.dat");
        FileReader fr = new FileReader(f);
        BufferedReader br = new BufferedReader(fr);
             int index = 0;
            int numWords =0;
            boolean prevwhitespace = true;
                    String line = br.readLine();
            while(index < line.length())
            {
                char c = line.charAt(index++);
                boolean currwhitespace = Character.isWhitespace(c);
                if(prevwhitespace && !currwhitespace)
                {
                    numWords++;
                }
                prevwhitespace= currwhitespace;
            }
                    
                   System.out.println("no. of words in file" +numWords);
        }
         public  void Charact()throws IOException
                 // counting characters
         {
             File f = new File("in.dat");
             FileReader fr = new FileReader(f);
        BufferedReader br = new BufferedReader(fr);
               long numChar = f.length();
               int countChar =0;
               while(numChar !=-1)
               {
                   countChar++;
               }
               System.out.println("no. of characters in the file =" + countChar);
              //  return countChar;  
                
    }
         public static void main(String args[])throws IOException
     {
         countCharacters ob1 = new countCharacters();
           ob1.Charact();
           ob1.Words();
           ob1.Lines();
        
     }
}

the file input.dat is
<br />
java programming for internet<br />
 javascript for web page development<br />
pearl for server side scripting
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: word/line/character count in files

 
0
  #2
Aug 30th, 2008
The problem is that your Charact method is stuck in the while loop. You obtain the length of the file and state the conditon numChar != -1.

Keep in mind what data types are for. They store a copy of the data they accept.

Here you're calling f.length() and placing it in numChar (once). Though I'm not very savvy on files, from what I understand you are only storing the file's (current) length once in numChar which means that your while loop will either never execute or execute infinitely since numChar != -1 will be true or false and in your while loop you are not manipulating or changing the data stored in numChar to cause an escape from the loop... therefore an infinite loop.


  1. long numChar = f.length();
  2. int countChar =0;
  3. while(numChar !=-1) // condition - will evaluate to true so long as numChar isn't -1
  4. {
  5. countChar++; // countChar changes, but numChar doesnt ^
  6. }

I made this change and it seemed to work, though I'm not entirely sure it's what you wanted to do--

  1. while(countChar < f.length()){
  2. countChar++;
  3. System.out.println("CharactWhileLoop");
  4. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 28
Reputation: newtechie is an unknown quantity at this point 
Solved Threads: 0
newtechie newtechie is offline Offline
Light Poster

Re: word/line/character count in files

 
0
  #3
Sep 1st, 2008
thanks alex edwards the character part is working along with no. of lines but the no. of words seems to give a wrong output.
can you guide me in that
thanks in advance
Last edited by newtechie; Sep 1st, 2008 at 1:29 pm. Reason: spelling
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: word/line/character count in files

 
0
  #4
Sep 1st, 2008
Originally Posted by newtechie View Post
thanks alex edwards the character part is working along with no. of lines but the no. of words seems to give a wrong output.
can you guide me in that
thanks in advance
Personally I'd read all of the characters into a String then tokenize the String based on the space character.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 28
Reputation: newtechie is an unknown quantity at this point 
Solved Threads: 0
newtechie newtechie is offline Offline
Light Poster

Re: word/line/character count in files

 
0
  #5
Sep 2nd, 2008
i modified the count word part and got the desired output
  1. /*
  2.  pgm to count the characters in a file,no. of lines in a file and no. of words
  3.  */
  4. import java.io.*;
  5. public class countCharacters
  6. {
  7. public void Lines()throws IOException
  8. {
  9. File f = new File("in.dat");
  10. FileReader fr = new FileReader(f);
  11. BufferedReader br = new BufferedReader(fr);
  12. String str = br.readLine();
  13.  
  14. LineNumberReader ln = new LineNumberReader(br);
  15. int count = 0;
  16. while (ln.readLine()!=null)
  17. {
  18. count++;
  19. }
  20.  
  21. System.out.println("no. of lines in the file = " + count);
  22.  
  23. }
  24. public void Words()throws IOException
  25. // count no. of words.
  26. {
  27. File f = new File("in.dat");
  28. FileReader fr = new FileReader(f);
  29. BufferedReader br = new BufferedReader(fr);
  30. StreamTokenizer stz = new StreamTokenizer(br);
  31. int index = 0;
  32. int numWords =0;
  33.  
  34. while(index !=StreamTokenizer.TT_EOF )
  35. {
  36. index =stz.nextToken();
  37. numWords++;
  38. }
  39.  
  40. System.out.println("no. of words in file = " + numWords);
  41. }
  42. public void Charact()throws IOException
  43. // counting characters
  44. {
  45. File f = new File("in.dat");
  46. FileReader fr = new FileReader(f);
  47. BufferedReader br = new BufferedReader(fr);
  48. long numChar = f.length();
  49. int countChar =0;
  50. while(countChar<numChar)
  51. {
  52. countChar++;
  53. }
  54. System.out.println("no. of characters in the file =" + countChar);
  55. // return countChar;
  56.  
  57. }
  58. public static void main(String args[])throws IOException
  59. {
  60. countCharacters ob1 = new countCharacters();
  61. ob1.Charact();
  62. ob1.Words();
  63. ob1.Lines();
  64.  
  65. }
  66. }
Last edited by cscgal; Sep 2nd, 2008 at 10:49 pm. Reason: Fixed code tags
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC