[英]Counting words in text file?
只是想说这个程序几乎在所有方面都打败了我。 我需要做的是输入一个.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);
}
在代码中,您具有以下内容:
// 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);
签出: 如何在System.in上使用多个Scanner对象?
您应该做什么:从第一台扫描仪取一行,然后用该行创建一个新的扫描仪
String s = in.nextLine();
Scanner sc = new Scanner(s);
现在以与第二个循环相同的方式进行迭代
准备和测试。
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
发表评论[如果]不适合您。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.