Invoking to type the team name twice instead of once [duplicate]

ε祈祈猫儿з 提交于 2019-12-25 19:59:10

问题


I have been asked to check if the team name is present in the text file I had created on my computer. I have written the complete code but the output is always invoking me type the name of the team twice before counting the number of times the team's name is present in the file. Please do see this, and let me know. Thanks.

import java.util.*;
import java.io.*;
public class worldSeries 
{
    public String getName(String teamName)
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Enter the Team Name : " );
        teamName = keyboard.nextLine();
        return teamName;
    }

    public int checkSeries1 () throws IOException
    {
        String teamName="";


        String[] winners = new String[50];
        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();

        int count = 0;
        String nameOfTeam = getName(teamName);

        for ( int  index = 0 ; index < winners.length ; index ++ )
        {
            if ( nameOfTeam.equals(winners[index]))
            {
                count++;

            }
        }
        return count;

    }

    public static void main(String[]Args)
    {
        String teamName = "";
        worldSeries object1 = new worldSeries();

        try
        {
            System.out.println(" The Number of times " + object1.getName(teamName) + "won the Championship is : " +object1.checkSeries1());
        }
        catch ( IOException ioe )

        {
            System.out.println(" Exception!!! ");

            ioe.printStackTrace();
        }

    }
}

回答1:


Count how many times you call getTeamName() -- you do it twice. And so you see it twice.

More importantly, the WorldSeries class should probably not have any Scanner objects or IO inside of it. Instead it should hold the WorldSeries information and have methods to check team names against the data. All user I/O should be done in your main method (in this program at least).




回答2:


Your code is doing this:

System.out.println(" The Number of times " + object1.getName(teamName) + "won the Championship is : " +object1.checkSeries1());

Your getName method is prompting for the name- it is being called directly in the line above, and it is also called indirectly (inside checkSeries1 on the same line). So that means it's being called twice in that line...

You will want to reconsider where prompting is done and do some refactoring to fix it.




回答3:


Here's a way to do this in Java:

import java.io.*;

public class WorldSeries
{
    /**
     * Count the number of lines a string occurs on in a file.
     */
    public static final void main(String[] argv)
        throws IOException
    {
        if(argv.length<2)
        {
            showUsage();
            System.exit(-1);
        }

        int count=0;
        String term = argv[0];
        String filename = argv[1];

        LineNumberReader reader = new LineNumberReader(
            new FileReader(filename)
            );

        for(String line=reader.readLine(); line != null; line=reader.readLine())
        {
            if(line.indexOf(term) > -1)
            {
                count++;
            }
        }

        System.out.println(count);
    }

    private static final void showUsage()
    {
        System.out.println("Search for term in a file.");
        System.out.println("USAGE: <term> <file-name>");
    }
}

But you can also do this with scripting:

grep -c "Boston Red Sox" world-series.txt


来源:https://stackoverflow.com/questions/22120122/invoking-to-type-the-team-name-twice-instead-of-once

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!