我有一个java程序,它读取一个txt文件并计算该文件中的单词。我设置了我的程序,因此从txt文件读取的String保存为ArrayList,我的变量字包含该ArrayList。我的代码的问题是我的if语句似乎没有在我的count变量中添加一个值,每次它检测到单词字符串中的空格时,它似乎只运行一次if语句。我如何才能使if语句找到一个空格,为我的计数器值添加+1,删除空格,并在单词变量的字符串中查找下一个空格?这是代码:
import java.io.*;
import java.util.*;
public class FrequencyCounting
{
public static void main(String[] args) throws FileNotFoundException
{
// Read-in text from a file and store each word and its
// frequency (count) in a collection.
Scanner inputFile = new Scanner(new File("phrases.txt"));
String word= " ";
Integer count = 0;
List<String> ma = new ArrayList<String>();
while(
inputFile.hasNextLine()) {
word = word + inputFile.nextLine() + " ";
}
ma.add(word);
System.out.println(ma);
if(word.contains(" ")) {
ma.remove(" ");
count++;
System.out.println("does contain");
}
else {
System.out.println("does not contain");
}
System.out.println(count);
//System.out.println(ma);
inputFile.close();
// Output each word, followed by a tab character, followed by the
// number of times the word appeared in the file. The words should
// be in alphabetical order.
; // TODO: Your code goes here.
}
}
当我执行程序时,变量计数的值为1,我从phrase.txt获得了txt文件的返回字符串表示
phrase.txt是:
my watch fell in the water
time to go to sleep
my time to go visit
watch out for low flying objects
great view from the room
the world is a stage
the force is with you
you are not a jedi yet
an offer you cannot refuse
are you talking to me
答案 0 :(得分:1)
你的if语句不在任何循环中,所以它只执行一次。
一种更好的方法,可以节省大量的运行时间,就像你已经做的那样读取每一行,使用String.split()方法在空格上拆分它,然后添加返回的String []的每个元素使用ArrayList.addAll()方法到列表中(如果存在,否则(可选地,确保容量和)逐个添加元素。)
然后使用ArrayList.size()方法计算元素数量。
答案 1 :(得分:0)
你的目标是什么?你只想阅读文件并计算单词数量吗?
答案 2 :(得分:0)
你需要使用while循环而不是只运行一次的if语句。这是做你想做的更好的方法:
Scanner inputFile = new Scanner(new File("phrases.txt"));
StringBuilder sb = new StringBuilder();
String line;
int totalCount = 0;
while(inputFile.hasNextLine()) {
line = inputFile.nextLine();
sb.append(line).append("\n"); // This is more efficient than concatenating strings
int spacesOnLine = countSpacesOnLine(line);
totalCount += spacesOnLine;
// print line and spacesOnLine if you wish to here
}
// print text file
System.out.println(sb.toString());
// print total spaces in file
System.out.println("Total spaces" + totalCount);
inputFile.close();
然后添加一个计算一行空格的方法:
private int countSpacesOnLine(String line) {
int totalSpaces = 0;
for(int i = 0; i < line.length(); i++) {
if (line.charAt(i) == ' ')
totalSpaces += 1;
}
return totalSpaces;
}
答案 3 :(得分:0)
您也可以使用以下一个班轮实现您的目标:
int words = Files.readAllLines(Paths.get("phrases.txt"), Charset.forName("UTF-8")).stream().mapToInt(string -> string.split(" ").length).sum();
答案 4 :(得分:0)
可能我迟到了,但这里是c#简单版:
else if()
答案 5 :(得分:0)
根据代码中的评论:
// Read-in text from a file and store each word and its
// frequency (count) in a collection.
// Output each word, followed by a tab character, followed by the
// number of times the word appeared in the file. The words should
// be in alphabetical order.
我的理解是你需要为每个单词存储计数,而不是总计单词数。为了存储应按字母顺序存储的每个单词的计数,最好使用TreeMap。
public static void main(String[] args) {
Map<String, Integer> wordMap = new TreeMap<String, Integer>();
try {
Scanner inputFile = new Scanner(new File("phrases.txt"));
while(inputFile.hasNextLine()){
String line = inputFile.nextLine();
String[] words = line.split(" ");
for(int i=0; i<words.length; i++){
String word = words[i].trim();
if(word.length()==0){
continue;
}
int count = 0;
if(wordMap.containsKey(word)){
count = wordMap.get(word);
}
count++;
wordMap.put(word, count);
}
}
inputFile.close();
for(Entry<String,Integer> entry : wordMap.entrySet()){
System.out.println(entry.getKey()+"\t"+entry.getValue());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}