[英]Counting number of lines, words, and characters in a text file
我試圖從用戶那里獲取輸入,並在文本文件中打印行數,單詞和字符。 但是,只有單詞的數量是正確的,它總是為行和字符打印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);
}
}
嘗試
int words = 0;
int lines = 0;
int chars = 0;
while(in.hasNextLine()) {
lines++;
String line = in.nextLine();
chars += line.length();
words += new StringTokenizer(line, " ,").countTokens();
}
in.next();
正在消耗第一個while()
中的所有行。 在第一個while循環結束后,輸入流中不再有要讀取的字符。
你應該在一個while循環計數行中嵌套你的角色和字數。
你認為有什么理由:
while(in.hasNext())
{
in.next();
words++;
}
不會消耗整個輸入流?
它會這么做,這意味着你的另外兩個while
循環永遠不會重復。 這就是為什么單詞和行的值仍然設置為零的原因。
你可能最好一次讀取一個字符的文件,每次通過循環增加字符數,並檢測字符以決定是否增加其他計數器。
基本上,無論你在哪里找到\\n
,增加行數 - 如果流中的最后一個字符不是\\n
你也應該這樣做。
而且,每當你從白色空間轉換到非白色空間時,增加字數(在流開始時可能會有一些棘手的邊緣情況處理,但這是一個實現問題)。
您正在查看類似以下偽代碼的內容:
# Init counters and last character
charCount = 0
wordCount = 0
lineCount = 0
lastChar = ' '
# Start loop.
currChar = getNextChar()
while currChar != EOF:
# Every character counts.
charCount++;
# Words only on whitespace transitions.
if isWhite(lastChar) && !isWhite(currChar):
wordCount++
# Lines only on newline characters.
if currChar == '\n':
lineCount++;
lastChar = currChar
currChar = getNextChar()
# Handle incomplete last line.
if lastChar != '\n':
lineCount++;
我認為最好的答案是
int words = 0;
int lines = 0;
int chars = 0;
while(in.hasNextLine()) {
lines++;
String line = in.nextLine();
for(int i=0;i<line.length();i++)
{
if(line.charAt(i)!=' ' && line.charAt(i)!='\n')
chars ++;
}
words += new StringTokenizer(line, " ,").countTokens();
}
執行第1個while時,文件指針被設置為文件的末尾。 嘗試這個:
Scanner in = new Scanner(file);
while(in.hasNext())
{
in.next();
words++;
}
in = new Scanner(file);
while(in.hasNextLine())
{
in.nextLine();
lines++;
}
in = new Scanner(file);
while(in.hasNextByte())
{
in.nextByte();
chars++;
}
我不是Java專家,但我認為.hasNext
, .hasNextLine
和.hasNextByte
都使用並遞增相同的文件位置指示符。 您需要通過創建一個新的Scanner作為Aashray提到,或者使用RandomAccessFile並調用file.seek(0);
來重置它file.seek(0);
每次循環后。
我同意@Cthulhu的回答。 在您的代碼中,您可以重置您的Scanner
對象( in
)。
in.reset();
這將在文件的第一行重置您的in對象。
您可以使用正則表達式來計算您的數量。
String subject = "First Line\n Second Line\nThird Line";
Matcher wordM = Pattern.compile("\\b\\S+?\\b").matcher(subject); //matches a word
Matcher charM = Pattern.compile(".").matcher(subject); //matches a character
Matcher newLineM = Pattern.compile("\\r?\\n").matcher(subject); //matches a linebreak
int words=0,chars=0,newLines=1; //newLines is initially 1 because the first line has no corresponding linebreak
while(wordM.find()) words++;
while(charM.find()) chars++;
while(newLineM.find()) newLines++;
System.out.println("Words: "+words);
System.out.println("Chars: "+chars);
System.out.println("Lines: "+newLines);
while(in.hasNextLine()) {
lines++;
String line = in.nextLine();
for(int i=0;i<line.length();i++)
{
if(line.charAt(i)!=' ' && line.charAt(i)!='\n')
chars ++;
}
words += new StringTokenizer(line, " ,;:.").countTokens();
}
import java.io.*;
class wordcount
{
public static int words=0;
public static int lines=0;
public static int chars=0;
public static void wc(InputStreamReader isr)throws IOException
{
int c=0;
boolean lastwhite=true;
while((c=isr.read())!=-1)
{
chars++;
if(c=='\n')
lines++;
if(c=='\t' || c==' ' || c=='\n')
++words;
if(chars!=0)
++chars;
}
}
public static void main(String[] args)
{
FileReader fr;
try
{
if(args.length==0)
{
wc(new InputStreamReader(System.in));
}
else
{
for(int i=0;i<args.length;i++)
{
fr=new FileReader(args[i]);
wc(fr);
}
}
}
catch(IOException ie)
{
return;
}
System.out.println(lines+" "+words+" "+chars);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.