Java练习(第2天)文本文档字符统计(Counting the Number of Words, Sentences, Characters, paragraphs in a Text File)

14 篇文章 0 订阅

一、问题描述

给定一个文本文档(.txt文件),编写一个Java程序,读取并输出该文本文档的单词数(单词之间用空格分隔)、句子数(句子用一般标点符号 !?.: 分隔)、字符数(包括字母、数字、空格、标点)、段落数(段与段之间有一空行)和空格数,共5行输出

问题难度系数:3/5

二、Java实现

import java.io.*;
public class Test 
{
	public static void main(String[] args) throws IOException
 	{
	 	File file = new File("./Input.txt");
	 	FileInputStream fileInputStream = new FileInputStream(file);
	 	InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
	 	BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
	 	
	 	String line;
	 	
	 	int wordCount = 0;
	 	int characterCount = 0;
	 	int paraCount = 0;
	 	int whiteSpaceCount = 0;
	 	int sentenceCount = 0;
	 	
	 	while ((line = bufferedReader.readLine()) != null)
	 	{
	 		if (line.equals(""))
		 	{
	 			paraCount += 1;
			}
			else
			{
				characterCount += line.length();
				String[] words = line.split("\\s+");
				wordCount += words.length;
				whiteSpaceCount += wordCount - 1;
				String[] sentence = line.split("[!?.:]+");
				sentenceCount += sentence.length;
			}
		}
		if (sentenceCount >= 1)
		{
			paraCount ++;
		}
			
	 	System.out.println("Total word count = " + wordCount);
		System.out.println("Total number of sentences = " + sentenceCount);
		System.out.println("Total number of characters = " + characterCount);
		System.out.println("Number of paragraphs = " + paraCount);
		System.out.println("Total number of whitespaces = " + whiteSpaceCount);
	}
}

三、样例输入、输出

样例输入(文本文档内容)

Donald Hardman (21 February 1899 – 2 March 1982) was a senior Royal Air Force (RAF) commander. He joined the Royal Flying Corps in 1917 and flew fighters over the Western Front, achieving nine victories to become a decorated ace. Between the wars he saw service with RAF squadrons in India and Egypt. At the outbreak of World War II, Hardman was a wing commander, attached to the Air Ministry. In 1944 he commanded No. 232 (Transport) Group during the Burma campaign. He served successively as Assistant Chief of the Air Staff, Commandant of RAF Staff College, Bracknell, and Air Officer Commanding-in-Chief of RAF Home Command. He was knighted in 1952. Hardman was Chief of the Air Staff of the Royal Australian Air Force from 1952 to 1954, and was responsible for reorganising its geographically based command-and-control system into a functional structure. After returning to Britain, he joined the Air Council in May 1954, and was promoted to air chief marshal the following year. He retired from the RAF in 1958.

样例输出

总单词数:171
总句子数:11
总字符数:1017
总段落数:1
总空格数:170
在这里插入图片描述

四、本文涉及知识点

1. Java 文件读取 操作;文件流FileInputStream类;按行读取文件 readLine() 方法;

2. 字符串 操作:求长度 .length() 方法、字符串分割 .split() 方法、字符串判等 .equals() 方法

3. Java文件输入输出类 Java.io.* ;输入输出异常类 IOException ;简单输入、输出操作。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不是AI

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值