2014-03-01 32 views
-4

我被要求檢查在我的計算機上創建的文本文件中是否存在團隊名稱。我編寫了完整的代碼,但輸出總是在我計算團隊名稱出現在文件中的次數之前輸入團隊名稱兩次。請看看這個,讓我知道。謝謝。調用鍵入球隊名稱兩次而不是一次

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

    } 
} 
+4

你爲什麼要張貼[類似的問題(HTTP://計算器.com/questions/22119883/again-and-again-prints-the-same-value)但是[不同名稱](http://stackoverflow.com/users/3339936/user3339936)? –

+0

只需在'getName()'方法中放置一個斷點,然後查看它何時被調用,然後使用調用堆棧(或僅僅跳出它)來查看它從哪裏調用。 – hyde

回答

1

計算你打了多少次getTeamName() - 你做了兩次。所以你看了兩遍。

更重要的是,WorldSeries類應該沒有任何Scanner對象或IO內部的IO。相反,它應該持有WorldSeries信息並且有方法根據數據檢查團隊名稱。所有的用戶I/O都應該在你的主要方法中完成(至少在這個程序中)。

+0

我沒做兩次。當我將它分配給String nameOfTeam時,我只看到它一次。 – user3369285

+0

@ user3369285:不要拿我的話來說,使用你的IDE來計算你調用它的次數。認真。該方法也不應該帶參數。 –

+0

我的教授要我在課堂上做,而不是在主要方法 – user3369285

1

您的代碼是這樣做的:

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

getName方法提示它被直接調用在上面的行中的名稱 - ,並且它也被稱爲間接(內部checkSeries1在同一行) 。所以這意味着它在該行中被調用兩次...

您將要重新考慮提示已完成的位置,並執行一些重構來修復它。

+0

你能告訴我,我可以做的改變,使其更好? – user3369285

+0

一個起點可能是把你的提示放到一個名爲'promptForName'的函數中,並使getName'變得更簡單?然後,您可以儘可能多地調用'getName'而不需要重新渲染。 – Krease

+0

1+爲好建議。 @ user3369285:你真的需要在這裏自己做一些思考。否則你永遠不會學習如何編寫代碼。 –

-1

這裏是一種在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>"); 
    } 
} 

但你也可以做到這一點使用腳本:

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

如果你打算在一次閱讀這個問題時對一個正確的答案進行投票,那麼如果你提供了對你的倒票的解釋,這將是很好的。 –

相關問題