2013-04-13 84 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定义的名称到另一个变量名或删除定义。