Question

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();
        }

    }
}
Was it helpful?

Solution 2

Instead of 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.

A method that just returns true / false could use the new method. If the returned value is 0, it returns false, else true.

OTHER TIPS

Basically you can reuse your already existing for loop

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.

Additionally I suggest to re-structure your code.

  1. Read the content of the file in separate method. Let the method return a String[] that is stored to String[] winners.
  2. Ask the user for the team name.
  3. Check whether the array winners contains the team name. This happens already in your method 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.
  4. Reuse the for-loop of checkSeries() in a new method that counts. This method needs only to be called if checkSeries() returned true.

You could use a HashMap with "key" as the word and "value" as the number of occurrences. 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(" ");

This will return an Array of string that contain all the words in your input. Then perform a loop on the Array and count the result of your team in the input.

If you want further help in writing the code, feel free to ask

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top