[英]How to count the number of times a word is in the text file
I have been asked to write a program that checks if the team name is present in the text file I had created on my computer and also say how many times it has been repeated on the file?我被要求编写一个程序来检查我在计算机上创建的文本文件中是否存在团队名称,并说明它在文件中重复了多少次? Can you please help me out?
你能帮我一下吗? I have written the code to check if the word is present but did not know how to check the number of times it is present.
我已经编写了代码来检查这个词是否存在,但不知道如何检查它出现的次数。
import java.util.*;
import java.io.*;
public class worldSeries
{
public boolean checkSeries () throws IOException
{
Scanner keyboard = new Scanner(System.in);
boolean result = false;
String[] winners = new String[200];
int i = 0 ;
File file = new File ("WorldSeriesWinners.txt");
Scanner inputFile = new Scanner(file);
while ( inputFile.hasNext () && i < winners.length )
{
winners[i] = inputFile.nextLine();
i++;
}
inputFile.close();
System.out.println(" Enter the Team's name you want to check : " );
String teamName = keyboard.nextLine();
for ( int index = 0 ; index < winners.length ; index ++ )
{
if ( teamName.equals(winners[index]))
{
result = true;
}
}
return result;
}
public static void main(String[]Args)
{
worldSeries object1 = new worldSeries();
try
{
System.out.println(" The search result for the Team's name is : " + object1.checkSeries ());
}
catch (IOException ioe)
{
System.out.println(" Exception!!! ");
ioe.printStackTrace();
}
}
}
Basically you can reuse your already existing for
loop基本上你可以重用你已经存在的
for
循环
for(int index = 0 ; index < winners.length ; index++){
if ( teamName.equals(winners[index])){
result = true;
}
}
Introduce a variable counter
that is initialized as 0 and incremented if the team name matches.引入一个变量
counter
,该counter
初始化为 0,如果团队名称匹配则递增。
Additionally I suggest to re-structure your code.此外,我建议重新构建您的代码。
String[]
that is stored to String[] winners
.String[]
存储到String[] winners
。winners
contains the team name.winners
是否包含团队名称。 This happens already in your method checkSeries()
.checkSeries()
。 As you can imagine from my previous comments, I would move the Scanner code to the method described in 1. Remember that a methods name should describe what's happening in the method - so checkSeries()
should only check and return a yes or no.checkSeries()
应该只检查并返回是或否。checkSeries()
in a new method that counts.checkSeries()
的 for 循环。 This method needs only to be called if checkSeries()
returned true.checkSeries()
返回 true 时才需要调用此方法。Instead of result = true;
而不是
result = true;
you could just increment a counter inside your loop.你可以在循环内增加一个计数器。
For example:例如:
int counter = 0;
for ( int index = 0 ; index < winners.length ; index ++ )
{
if ( teamName.equals(winners[index]))
{
counter = counter + 1;
}
}
And you could then change the return type of your method to int
and return counter
.然后您可以将方法的返回类型更改为
int
并返回counter
。
A method that just returns true / false could use the new method.只返回 true / false 的方法可以使用新方法。 If the returned value is 0, it returns false, else true.
如果返回值为 0,则返回 false,否则返回 true。
You could use a HashMap with "key" as the word and "value" as the number of occurrences.您可以使用以“key”作为单词和“value”作为出现次数的 HashMap。 Whenever you read a word check whether the word is available in the map and increment its value.
每当您阅读一个单词时,请检查该单词是否在地图中可用并增加其值。 Let know if you need a code example.
如果您需要代码示例,请告诉我们。
If I were you, I would read all the text input once in a string
then split it with the in between white spaces using the method string.split(" ");
如果我是你,我会在一个
string
读取一次输入的所有文本,然后使用方法string.split(" ");
This will return an Array of string
that contain all the words in your input.这将返回一个包含输入中所有单词的
string
数组。 Then perform a loop on the Array and count the result of your team in the input.然后对 Array 执行循环并在输入中计算您团队的结果。
If you want further help in writing the code, feel free to ask如果您在编写代码方面需要进一步帮助,请随时询问
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.