| | |
word/line/character count in files
Thread Solved
![]() |
•
•
Join Date: Jul 2008
Posts: 28
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"); }
Personally I'd read all of the characters into a String then tokenize the String based on the space character.
•
•
Join Date: Jul 2008
Posts: 28
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 10:49 pm. Reason: Fixed code tags
![]() |
Similar Threads
- Tutorial: Automating Office applications (Visual Basic 4 / 5 / 6)
- 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
| Thread Tools | Search this Thread |
account android applet application apps array automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) card class classes client code collision columns component constructor database designadrawingapplicationusingjavajslider draw eclipse error eventlistener exception expand fractal free game givemetehcodez graphics gui guidancer homework html ide image inheritance integer integration intellij j2me java javafx javamicroeditionuseofmotionsensor javaprojects jlabel jme jni jpanel jtextfield jtree julia linux loop method midlethttpconnection migrate mobile mobiledevelopmentcreatejar monitoring myaggfun netbeans newbie nullpointerexception open-source oracle plazmic print problem program property ria scanner server set sharepoint smart sms smsspam sourcelabs splash sql sqlite subclass support swing testautomation textfield threads tree trolltech unlimited utility windows





