2013-03-06 163 views
4

我想从用户处获取输入,并在文本文件中输出行数,字数和字符数。但是,只有字数是正确的,它总是为行和字符打印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); 
    } 
} 

回答

2

in.next();正在消耗第一个while()中的所有行。在第一个while循环结束后,输入流中不会再有字符被读取。

你应该巢穴你的性格和字数一个while循环计数线。

1

是有一些原因,你认为:

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++; 
0

文件指针设置为文件的末尾。试试这个:

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++; 
     } 
+0

的工作,但不正确的做法IMO – 2013-03-06 05:08:37

+0

筑巢它将有其自身的问题。条件需要改变。 – Aashray 2013-03-06 05:10:13

0

我不是Java专家,但我会推定该.hasNext.hasNextLine.hasNextByte全部使用,并增加相同的文件位置指示器。您需要重置该设置,或者通过创建一个新的扫描器作为Aashray提到的方法,或者使用RandomAccessFile并在每个循环之后调用file.seek(0);

6

尝试

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(); 
    } 
+0

谢谢,这有助于 – user2138453 2013-03-06 05:21:58

+0

好,请注意,我们可以以不同的方式计算单词,我用','和''作为单词分隔符,但您可以更改它 – 2013-03-06 05:25:36

0

我@Cthulhu答案达成一致。在您的代码中,您可以重置您的Scanner对象(in)。

in.reset(); 

这会在您的文件的第一行重置您的对象。

0

您可以使用正则表达式来计算。

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); 
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

我认为最好的答案是

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(); 
} 
0
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); 
    } 
} 
+0

请尝试格式化您的答案。要开始,对于代码,使用4个空格缩进。有关更多信息,请访问http://stackoverflow.com/help/formatting – 2016-11-27 04:07:09