I have been asked to check the number of times a team\'s name is on the text that is on my computer. I wrote the code, the code works fine by counting the number of times th
Don't call 'GetName' in the loop, call it once before the loop and store the result.
Calling getName()
once every loop will cause the program to ask for a team name every loop:
int count = 0;
for ( int index = 0 ; index < winners.length ; index ++ )
{
if ( getName(teamName).equals(winners[index]))
{
count++;
}
}
By moving getName()
out of the loop, it will only be called once (and a team name will only be requested once):
int count = 0;
String nameOfTeam = getName(teamName); // This line runs getName() once
for ( int index = 0 ; index < winners.length ; index ++ )
{
if ( nameOfTeam.equals(winners[index]))
{
count++;
}
}
In the method checkSeries1()
remove the method call for getName(teamName) out of for loop and call getName() only once outside for loop, like this:
int count = 0;
String name = getName(teamName);
for ( int index = 0 ; index < winners.length ; index ++ )
{
if ( name.equals(winners[index]))
{
count++;
}
}