Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Character Count-java

$
0
0
Hi I am suppose to find character, word and line count. Line and word count work but the character count doesn't work. I am suppose to read in a external txt file. This is what I don't get. I am suppose to get a word count of 2158. But i'm getting 2106. But the difference between them is 52 and that is the line count. When I go to an office suite and check the character count it says 2106. I'm sooooo confused. This driving me nuts please help!

**I think I am suppose to count the newline count. But I don't know how.

  File file = new File("cat.txt");
      Scanner scanner = new Scanner (file);
      int wordCount=0;
      int lineCount=0;
      int charCount=0;
      int counter=0;
      int lineLength=0;
      while(scanner.hasNextLine())
      {
        String line = scanner.nextLine();
        lineCount++;
        Scanner wordScanner = new Scanner(line);
        line.replaceAll("\n"," ");
                lineLength= line.length();
                charCount+=lineLength;
        while(wordScanner.hasNext())
        {
          String word = wordScanner.next();
          wordCount++;
         
         
        }
      }

     System.out.println(lineCount+" "+wordCount+" "+charCount+" "+file);

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>