2013-04-13 83 views
0

所以我今天一直在努力嘗試創建一個名爲'Sport'的類的實例。 我有我的代碼集,所以我跑了用戶界面,然後運行一個構造函數,然後運行它從一個文本文件加載體育價值觀的另一個構造函數。Java對象實例創建問題

的問題是,我顯然是創建對象的方式是錯誤的。真的可以使用一些幫助。

public static void seperateValues(String sportDetail) 
{ 

    String[] sportDetails = sportDetail.split(","); 
    System.out.println("Adding new sport to the Sport collection"); 
    System.out.println(sportDetail); 

    /* 
    for(int i=0; i<sportDetails.length; i++) //just used for testing whether it was splitting correctly 
    { 
     System.out.println(sportDetails[i]); 
    } */ 

    // name,usagefee,insurance,affiliationfees, then court numbers 
    //Tennis,44,10,93,10,11,12,13,14,15,16 
    int vlength; 
    vlength = sportDetail.length(); 

    String[] sportDetailz; 
    sportDetailz = new String[vlength];  
    sportDetailz[0] = sportDetails[0]; //name 
    sportDetailz[1] = sportDetails[1]; //usage fees 
    sportDetailz[2] = sportDetails[2]; //insurance 
    sportDetailz[3] = sportDetails[3]; //afflcationfees 

    String vSportObjectName; 
    vSportObjectName = sportDetails[0]; 

    String sportinstance; 
    sportinstance = sportDetails[0]; //this is the name of the sport which I'm hoping each loop around 
    //it will give a new name to 
    Sport sportinstance = new Sport(sportDetails); 
    //System.out.println(Sport.this.name); 

} 

錯誤消息:variable sportinstance is already defined in method seperateValues(java.lang.String)

http://puu.sh/2zil9

+0

請提供錯誤消息的問題文本,而不是圖像。 – Kninnug

+1

您正在重新使用變量名稱;別。 –

回答

3

我猜你的問題是,你首先聲明sportinstanceString。然後嘗試再次將其定義爲Sport

只是刪除以下行,然後再試一次(因爲它看起來並不像他們實際上是在其他任何地方使用):

String sportinstance; 
sportinstance = sportDetails[0]; 

另一種選擇是簡單地重命名的sportinstance實例中的任一。

+0

我試圖讓我的程序工作,以便每次方法循環時,它創建一個新名稱的對象實例。所以每次運動都是一項新的運動。 要麼是「網球」或「足球」 – user2277729

1

您試圖將sportinstance定義爲兩種不同的數據類型,Java不允許這樣做。要麼改變的sportinstanceSport定義的名稱到另一個變量名或刪除定義。