我想從用戶處獲取輸入,並在文本文件中輸出行數,字數和字符數。但是,只有字數是正確的,它總是爲行和字符打印0。計算文本文件中的行數,字數和字符數
import java.util.*;
import java.io.*;
public class TextFileInfoPrinter
{
public static void main(String[]args) throws FileNotFoundException
{
Scanner console = new Scanner(System.in);
System.out.println("File to be read: ");
String inputFile = console.next();
File file = new File(inputFile);
Scanner in = new Scanner(file);
int words = 0;
int lines = 0;
int chars = 0;
while(in.hasNext())
{
in.next();
words++;
}
while(in.hasNextLine())
{
in.nextLine();
lines++;
}
while(in.hasNextByte())
{
in.nextByte();
chars++;
}
System.out.println("Number of lines: " + lines);
System.out.println("Number of words: " + words);
System.out.println("Number of characters: " + chars);
}
}
的工作,但不正確的做法IMO – 2013-03-06 05:08:37
築巢它將有其自身的問題。條件需要改變。 – Aashray 2013-03-06 05:10:13