word/line/character count in files
Please support our Java advertiser: Programming Forums
Thread Solved
![]() |
•
•
Posts: 18
Reputation:
Solved Threads: 0
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
the file input.dat is
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
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.
I made this change and it seemed to work, though I'm not entirely sure it's what you wanted to do--
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)
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--
java Syntax (Toggle Plain Text)
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
Personally I'd read all of the characters into a String then tokenize the String based on the space character.
•
•
Posts: 18
Reputation:
Solved Threads: 0
i modified the count word part and got the desired output
java Syntax (Toggle Plain Text)
/* 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(); } }
Last edited by cscgal : Sep 2nd, 2008 at 9:49 pm. Reason: Fixed code tags
![]() |
Similar Threads
Other Threads in the Java Forum
- Automating Office applications (Visual Basic Tutorials)
- Pointers (archived tutorial) (C++)
- project plz who can help me (C++)
- Open In New Window Php (PHP)
- who can help me in c++ project (C++)
- Help (Python)
Other Threads in the Java Forum
- Previous Thread: Need Code for To-Do list in java with mysql
- Next Thread: Help
•
•
•
•
Views: 3283 | Replies: 4 | Currently Viewing: 1 (0 members and 1 guests)





Linear Mode