字数

Character Count

我正在计算字数、行数和字符数(不包括空格)。我无法开始工作的唯一部分是忽略字符数的空格。

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class Exercise2 {
public static void main(String[] args) throws IOException{
    File file = getValidFile();
    int count = wordCount(file);
    int lines = lineCount(file);
    int characters = characterCount(file);
    System.out.println("Total Words = " + count);
    System.out.println("Total Lines = " + lines);
    System.out.println("Total Characters = " + characters);

}
public static int characterCount(File file) throws IOException {
    {
        Scanner inputFile = new Scanner(file).useDelimiter(",\s*");;
        int characters = 0; // initialise the counter variable
        while (inputFile.hasNext())
        {
            inputFile.next(); //read in a word
            characters++; //count the word
        }
        inputFile.close();
        return characters;
    }
}
public static int lineCount(File file)throws IOException {
    {
        Scanner inputFile = new Scanner(file);
        int lines = 0; // initialise the counter variable
        while (inputFile.hasNext())
        {
            inputFile.nextLine(); //read in a line
            lines++; //count the line
        }
        inputFile.close();
        return lines;
    }
}
public static int wordCount(File file) throws IOException {
    {
        Scanner inputFile = new Scanner(file);
        int count = 0; // initialise the counter variable
        while (inputFile.hasNext())
        {
            inputFile.next(); //read in a word
            count++; //count the word
        }
        inputFile.close();
        return count;
    }
}
public static File getValidFile() 
{

    String filename;     // The name of the file
    File file;

    // Create a Scanner object for keyboard input.
    Scanner keyboard = new Scanner(System.in);

    // Get a valid file name.
    do
    {
        /*for (int i = 0; i < 2; i ++ )
            {*/
        System.out.print("Enter the name of a file: ");
        filename = keyboard.nextLine();
        file = new File(filename);
        if (!file.exists())
            System.out.println("The specifed file does not exist - please try again!");

    }while( !file.exists());
    return file;

}
}

如果要计算文件中的字符数,不包括任何空格,您可以逐行读取文件并累加字符数,或者读取整个文件a String 并计算字符数,例如

String content = new Scanner(file).useDelimiter("\Z").next();
int count = 0;
for (int i = 0; i < content.length(); i++) {
    if (!Character.isWhitespace(content.charAt(i))) {
        count++;
    }
}
System.out.println(count);

编辑 其他解决方案如果你不关心文件的内容,那么就没有必要把它加载到一个String中,你可以一个字符一个字符地读取。

计算 Arthur Rimbaud 诗歌中非空白字符的示例。

使用 Scanner

    URL rimbaud = new URL("http://www.gutenberg.org/cache/epub/29302/pg29302.txt");
    int count = 0;
    try (BufferedReader in = new BufferedReader(new InputStreamReader(rimbaud.openStream()))) {
        int c;
        while ((c = in.read()) != -1) {
            if (!Character.isWhitespace(c)) {
                count++;
            }
        }
    }
    System.out.println(count);

使用普通的StreamReader

    count = 0;
    try (Scanner sin = new Scanner(new BufferedReader(new InputStreamReader(rimbaud.openStream())))) {
        sin.useDelimiter("");
        char c;
        while (sin.hasNext()) {
            c = sin.next().charAt(0);
            if (!Character.isWhitespace(c)) {
                count++;
            }
        }
    }
    System.out.println(count);