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
[TEX]
java programming for internet
javascript for web page development
pearl for server side scripting[/TEX]

Recommended Answers

All 4 Replies

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.

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

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

while(countChar < f.length()){
			countChar++;
			System.out.println("CharactWhileLoop");
		}

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

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.

i modified the count word part and got the desired output

/*
 pgm to count the characters in a file,no. of lines in a file and no. of words
 */
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);
        StreamTokenizer stz = new StreamTokenizer(br);
             int index = 0;
            int numWords =0;
            
            while(index !=StreamTokenizer.TT_EOF )
            {
                index =stz.nextToken();
                numWords++;
            }
                    
                   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(countChar<numChar)
               {
                   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();
        
     }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.