View Single Post
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