943,847 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 21841
  • Java RSS
Aug 30th, 2008
0

word/line/character count in files

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
newtechie is offline Offline
33 posts
since Jul 2008
Aug 30th, 2008
0

Re: word/line/character count in files

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.


java Syntax (Toggle Plain Text)
  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--

java Syntax (Toggle Plain Text)
  1. while(countChar < f.length()){
  2. countChar++;
  3. System.out.println("CharactWhileLoop");
  4. }
Reputation Points: 392
Solved Threads: 108
Posting Shark
Alex Edwards is offline Offline
971 posts
since Jun 2008
Sep 1st, 2008
0

Re: word/line/character count in files

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
Reputation Points: 10
Solved Threads: 0
Light Poster
newtechie is offline Offline
33 posts
since Jul 2008
Sep 1st, 2008
0

Re: word/line/character count in files

Click to Expand / Collapse  Quote originally posted by newtechie ...
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.
Reputation Points: 392
Solved Threads: 108
Posting Shark
Alex Edwards is offline Offline
971 posts
since Jun 2008
Sep 2nd, 2008
0

Re: word/line/character count in files

i modified the count word part and got the desired output
java Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Light Poster
newtechie is offline Offline
33 posts
since Jul 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Need Code for To-Do list in java with mysql
Next Thread in Java Forum Timeline: Help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC