如何编此程序 java

来源:百度知道 编辑:UC知道 时间:2024/06/23 07:24:53
Write a program that reads a text le and outputs the following statistics: the average line
length, the longest two lines, the average word length, and the longest word. A word is de ned
as a maximal sequence of consecutive characters that are not white spaces. The static function
boolean Character.isWhitespace(char) returns true if the input character is a white space.
The program, invoked as java fileStatistics < filename >, returns the required statis-
tics with appropriate messages for the user.

看明白了,按照A word is defined
as a maximal sequence of consecutive characters that are not white spaces. The static function
boolean Character.isWhitespace(char) returns true if the input character is a white space.
这两句话来说呢,实际上是使用了比较复杂的自定义方法,而抛弃了java一些现有的方法。
package file;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class FileContentsCheck {

public static String fileStatistics(String fileName) throws IOException {
// 行的总长度
int totalLineLength = 0;
// 行数
int lineCount = 0;
// 最长行
String[] lines = new String[2];
// 单词总长度
int totalWordLength = 0;
// 单词总数
int wordCount = 0;
// longest word
List<String> longestWordList = new ArrayList<String>();
BufferedReader bReader = new BufferedReader(new FileReader(fileName));