计算文本文件中的单词?

时间:2015-02-11 19:21:46

标签: java

只是想说这个程序几乎在所有方面都打败了我。 我需要做的是输入一个包含此

的.txt文件
  

应该有     59个字符     13个字     和     6行     在这个文件中。

不知道如何将其格式化为6行。

该程序对行进行计数,但表示文本文件中有78个单词,而不是13.这是6 * 13 = 78。

如何才能让它只读取一次...因此读取13个字而不是13 * 6。 该计划似乎多次计算这些词。 (评论由专业人士提供指导)。

package inputoutput;

import java.util.*;
import java.io.*;

public class input {

public static void main(String[] args) throws FileNotFoundException {
    Scanner s = new Scanner(System.in);
    String name;
    int lineCount = 0;
    int wordCount = 0;


    System.out.println("Please type the file you want to read in: ");
    name = s.nextLine();



    // create a Scanner object to read the actual text file
    File input = new File("C:\\Users\\Ceri\\workspace1\\inputoutput\\src\\inputoutput\\" + name + ".txt");
    Scanner in = new Scanner(input);


    while (in.hasNextLine()){ // enter while loop if there is a complete line available

        // increase line counter variable
        lineCount++;


        // read in next line using Scanner object, inFile
        in.nextLine();

        // create another Scanner object in order to scan the line word by word
        Scanner word = new Scanner(input); 


        // use while loop to scan individual words, increase word counter
        while(word.hasNext()){
            // increase word counter
            wordCount++;    
            // Scanner move to next word
            word.next();
        }
        word.close();
        // count number of chars including space but not line break by using the length() method of the String class and increment character counter

    }

    in.close();
    System.out.print("Lines " + lineCount + " "+ "Words " +  wordCount);

}

3 个答案:

答案 0 :(得分:2)

在您的代码中,您有以下内容:

// read in next line using Scanner object, inFile
in.nextLine();

// create another Scanner object in order to scan the line word by word
Scanner word = new Scanner(input); 

而不是,改为:

// read in next line using Scanner object, inFile
String nextline = in.nextLine();

// create another Scanner object in order to scan the line word by
// word
Scanner word = new Scanner(nextline);

答案 1 :(得分:1)

检查出来:How to use multiple Scanner objects on System.in?

您应该做什么:从第一台扫描仪中取一行然后用该行创建一个新的扫描仪

String s = in.nextLine();
Scanner sc = new Scanner(s);

现在以与第二个循环相同的方式迭代

答案 2 :(得分:-1)

准备好并经过测试。

public class input {

 public static void main(String[] args) throws FileNotFoundException {
   Scanner s = new Scanner(System.in);
   String name;
   int lineCount = 0;
    int wordCount = 0;


System.out.println("Please type the file you want to read in: ");
name = s.nextLine();



  // create a Scanner object to read the actual text file
File input = new File("C:\\Users\\Ceri\\workspace1\\inputoutput \\src\\inputoutput\\" + name + ".txt");
s.close();

//--------------------
   //Count how many lines
Scanner in = new Scanner(input);
while (in.hasNextLine()){ 

    // increase line counter variable
    ++lineCount;
    in.nextLine();
} 
in.close(); //Close [in] Scanner



//------------------------------    
 //Count how many words
Scanner word = new Scanner(input); 
while(word.hasNext()){

 // increase word counter variable
    ++wordCount;
    word.next();
}
 word.close(); //close [word] Scanner



System.out.print("Lines " + lineCount + " "+ "Words " +  wordCount);

 }//end of main Method


}//end of Class

发表评论[if]它不适合你。