字符数

时间:2015-03-18 20:18:15

标签: java java.util.scanner

我试图计算单词,行数和字符数(不包括空格)。我无法开始工作的唯一部分是忽略字符数的空白。

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;

}
}

1 个答案:

答案 0 :(得分:0)

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

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); ,您只需按字符阅读即可。

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

使用String

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