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

Help: Program Counting Whitespaces!

$
0
0
Hi, i'm working on a program that takes in a .txt file, and counts the number of words, lines, and characters in that .txt file. I have my program working, kinda sorta almost! I am getting the correct number of words, and lines, but my character count is including whitespace! I cant find anything in my book on it so I need some help! Heres my Code, the contents of my test .txt file, and my output!

.txt file:

Hey, this is a test!
Java Programming.

output:
Words: 9
Lines: 2
Characters: 39

Code:
package mat2670;

import java.io.*;
import java.util.*;

public class CheckPaper {

    private static int wordCount;
    private static int lineCount;

    public static void main(String[] args) throws FileNotFoundException {
        Scanner inputLine = new Scanner(new File("essay.txt"));
        File f = new File("essay.txt");

//words(inputLine);
        lines(inputLine);

        System.out.println("Words: " + wordCount);
        System.out.println("Lines: " + lineCount);
        System.out.println("Characters: " + f.length());
    }

    public static int words(String input) {
        String[] wrds = input.split(" ");
        wordCount += wrds.length + 1;

        return wordCount;
    }

    public static int lines(Scanner input) {
        while (input.hasNextLine()) {
            String line = input.nextLine();
            words(line);
            lineCount++;
        }

        return lineCount;
    }
}


Viewing all articles
Browse latest Browse all 51036

Trending Articles



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