【发布时间】:2014-04-02 22:17:10
【问题描述】:
我被要求检查团队名称是否存在于我在计算机上创建的文本文件中。我已经编写了完整的代码,但输出总是要求我在计算团队名称出现在文件中的次数之前输入两次团队名称。请务必看到这个,并让我知道。谢谢。
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();
}
}
}
【问题讨论】:
-
你为什么在similar question下发布different name??
-
只要在
getName()方法中放一个断点,看看什么时候被调用,然后使用调用栈(或者直接跳出来)看看是从哪里调用的。