2013-04-13 4 views
0

저는 오늘 하루 종일 '스포츠'라는 클래스의 인스턴스를 만들려고 애 쓰고 있습니다. 사용자 인터페이스를 실행하여 생성자를 실행 한 다음 텍스트 파일에서 Sport 값을로드하는 다른 생성자를 실행하도록 코드를 설정했습니다.자바 객체 인스턴스 생성 문제

문제는 분명히 개체를 만드는 방식이 잘못되었습니다. 정말로 도움이 될지도 몰라.

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

당신이 먼저 String으로 sportinstance를 선언한다는 것입니다. 그런 다음 다시 Sport으로 정의하십시오.

그냥 다음 줄을 제거하고 (실제로 다른 곳에서 사용되는 것처럼 보이지 않는) 다시 시도 :

String sportinstance; 
sportinstance = sportDetails[0]; 

다른 옵션은 단순히 sportinstance의 인스턴스 중 하나의 이름을 변경하는 것 .

+0

나는 프로그램이 동작 할 때마다 메서드가 반복 될 때마다 새로운 이름으로 객체 인스턴스를 생성한다. 그래서 '스포츠 인스 턴 스'는 매회 새로운 스포츠입니다. '테니스'또는 '축구'중 하나 – user2277729

1

sportinstance을 두 개의 다른 데이터 유형으로 정의하려고하고 있으며 Java는이를 허용하지 않습니다. Sport 정의의 이름을 sportinstance으로 변경하거나 다른 변수 이름을 변경하거나 정의를 제거하십시오.