Results 1 to 4 of 4

Thread: Getting file character count

  1. #1

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Question Getting file character count

    Im trying to get the character count of a file which contains only my name "Brandon". The problem is that i am getting two different results for the character count. The first System.out.println() returns the correct count and the second a result of 11 characters. This makes no sense to me. Any help would be appreciated. Thanks.
    Code:
     public int getFileCharCount(String file) {
    
        String lineOnebyOne = null;
        String finalText = null;
        int numberOfChar = 0;
      try{
        BufferedReader br = new BufferedReader(new FileReader(file));
        while((lineOnebyOne = br.readLine()) != null){
         
          finalText += lineOnebyOne;
            System.out.println(lineOnebyOne.length());
             System.out.println(finalText.length());
        }
         
          numberOfChar = finalText.length();
         br.close();
         }catch(IOException e){System.err.println(e + "getFileCharCount");}
          
           return numberOfChar;
       }

  2. #2

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Never mind i got it. Any reason why concatenating Strings would give me an off count?
    Code:
    public int getFileCharCount(String file) {
    
        String lineOnebyOne = null;
        StringBuffer finalText = new StringBuffer();
        int numberOfChar = 0;
    
       try{
        BufferedReader br = new BufferedReader(new FileReader(file));
        while((lineOnebyOne = br.readLine()) != null){
         
           finalText.append(lineOnebyOne);
            System.out.println(lineOnebyOne.length());
             System.out.println(finalText.length());
        }
         
          numberOfChar = finalText.length();
         br.close();
         }catch(IOException e){System.err.println(e + "getFileCharCount");}
          
           return numberOfChar;
       }

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    I think you lose the newlines, but that's a guess. Try outputting the single lines and the final string to check.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  4. #4

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    The problem is that there is only one line of text. I dont see what the difference would be between this finalText += lineOnebyOne; and this finalText.append(lineOnebyOne); It should result in the same thing. Right?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width