我正在开发一个程序,它读取文本文件并打印行数,最长行,每行上的令牌数以及每行上最长令牌的长度。
我在阅读最长的单词时遇到问题,我的程序给了我每行的字母数,而不是只有最长单词的字母数!!!
Beware the Jabberwock, my son,
the jaws that bite, the claws that catch,
Beware the JubJub bird and shun
the frumious bandersnatch.
Line 1 has 5 tokens (longest = 11)
Line 2 has 8 tokens (longest = 6)
Line 3 has 6 tokens (longest = 6)
Line 4 has 3 tokens (longest = 13)
Longest line : the jaws that bite, the claws that catch,
Line 1 has 5 tokens (longest = 31)
Line 1 has 8 tokens (longest = 42)
Line 1 has 6 tokens (longest = 32)
Line 1 has 3 tokens (longest = 27)
import java.io.*;
import java.util.*;
public class InputStats{
public static void main (String [] args )
throws FileNotFoundException{
Scanner console = new Scanner ( System.in);
System.out.println(" ");
System.out.println();
System.out.println("Please enter a name of a file " );
String name = console.nextLine();
Scanner input = new Scanner ( new File (name));
while (input.hasNextLine()) {
String line = input.nextLine();
inputStats(new Scanner(line));
}
}//end of amin
public static void inputStats (Scanner input)
throws FileNotFoundException{
int numLines=0;
int numwords=0;
String maxLine = "";
while (input.hasNextLine()){
String next = input.nextLine();
numwords += next.split("\\s+").length;
numLines++;
if (next.length() > maxLine.length()) {
maxLine = next;
}
}//end of while
System.out.print("Line " + numLines + " has ");
System.out.print(numwords + "tokens " );
System.out.print("(longest = " + maxLine.length()+ ")");
System.out.println();
}//end of method
}// end of class
答案 0 :(得分:0)
如果“令牌”不是字母,那么这将为您做到:
public static void main(final String... args)
throws IOException
{
String fileName;
System.out.printf("Please enter the name of a file\n");
try (
final Scanner scanner = new Scanner(System.in);
) {
fileName = scanner.nextLine();
}
// If we reached this point, good; .nextLine() only fails with unchecked
// exceptions, so if it failed we won't be here. And the scanner will be closed.
final Path path = Paths.get(fileName);
int lineCount = 0;
for (final String line: Files.readAllLines(path, StandardCharsets.UTF_8)) {
lineCount++;
analyzeLine(line);
}
System.out.printf("TOTAL: %d lines\n", lineCount);
}
private static void analyzeLine(final String line)
{
final String[] tokens = line.split("\\W+");
int largestToken = 0;
for (final String token: tokens)
if (token.length() > largestToken)
largestToken = token.length();
System.out.printf("This line: %d tokens, largest token has length %d\n",
token.length, largestToken);
}
答案 1 :(得分:0)
Ty This:
import java.io.*;
import java.util.*;
class InputStats
{
static String lines="";
static String bigestline="";
public static void main (String [] args )
throws FileNotFoundException
{
Scanner console = new Scanner ( System.in);
System.out.println(" ");
System.out.println();
System.out.println("Please enter a name of a file " );
String name = console.nextLine();
Scanner input = new Scanner ( new File (name));
while (input.hasNextLine()) {
String line = input.nextLine();
inputStats(new Scanner(line));
}
String[] line=lines.split(":");
bigestline=line[0];
for(int j=0;j<line.length-1;j++)
{
if(bigestline.length()<line[j+1].length())
{
bigestline=line[j+1];
}
}
System.out.print("\n Biggest Line in File="+bigestline+"\n");
} //end of amin
public static void inputStats (Scanner input)
throws FileNotFoundException{
int numLines=0;
int numwords=0;
String maxLine = "";
while (input.hasNextLine())
{
String next = input.nextLine();
System.out.print("\n"+next);
lines=lines+":"+next;
numwords += next.split("\\s+").length;
numLines++;
if (next.length() > maxLine.length()) {
maxLine = next;
}
}
System.out.print("\nLine " + numLines + " has ");
System.out.print(numwords + "tokens " );
System.out.print("(longest = " + maxLine.length()+ ")");
System.out.println();
}
}