2014-03-04 2 views
0

안녕하십니까, 쉬운 오후가 있습니다. 임 try-catch 루프 내부에서 배열의 내용을 가져 와서 루프 외부에 선언 된 배열 안에 배치하는 데 문제가 있습니다. 이 작업을 수행하려는 이유는 외부의 주 기능에서 호출 할 수 있기 때문입니다. 어떤 포인터? try-catch 루프 내부에서 배열 가져 오기

내 코드를 살펴 유무 : 나는 배열 votesPerPosition []의 내용을 얻을 ArrayVotesPosition [] 안에 넣어하려고

을;

모든 포인터? 감사! 귀하의 도움을 많이 주시면 감사하겠습니다.

public class Vote{ 
int voter = 0; 
Scanner sc; 
BufferedReader br; 
String line; 
FileInputStream fis; 
public int j = 0; 
private int voterCount = 0; 
public Arrays VotesPosition[]; 



public Vote() { 
    sc = new Scanner(System.in); 
    System.out.println("Enter file name > "); 
    String filename = sc.next(); 
    try { 
     br = new BufferedReader(new InputStreamReader(new FileInputStream(filename))); 
    } catch (FileNotFoundException ex) { 
     //Logger.getLogger(Vote.class.getName()).log(Level.SEVERE, null, ex); 
    } 
    do{ 
    voter++; 
     try { 
      filename = br.readLine(); 
     } catch (IOException ex) { 
      Logger.getLogger(Vote.class.getName()).log(Level.SEVERE, null, ex); 
     } 
     String votesPerPosition[] = filename.split(","); 
     for(String vote: votesPerPosition){ 
      Integer.parseInt(vote); 
      for (j = 0; j < votesPerPosition.length; j++);{ 
      System.out.println("voter " + voter + "voted for candidate #" + vote); 
      voterCount++; 
     } 
     } 
     return String votesPerPosition; //<-------------- This is what I am trying//     
    }while(filename != null); 

} 

/** 
* @return the voterCount 
*/ 
public int getVoterCount() { 
    return voterCount; 
} 

/** 
* @param voterCount the voterCount to set 
*/ 
public void setVoterCount(int voterCount) { 
    this.voterCount = voterCount; 
} 

/** 
* @return the VotesPosition 
*/ 
public Arrays[] getVotesPosition() { 
    return VotesPosition; 
} 

/** 
* @param VotesPosition the VotesPosition to set 
*/ 
public void setVotesPosition(Arrays[] VotesPosition) { 
    this.VotesPosition = VotesPosition; 
} 

} 

답변

0
public String[] Vote()으로 기능을 변경하고 데이터를 반환 return votesPerPosition;를 사용

.

(String[]은 자바에서 Object 유형으로 계산 된 참조이므로 함수 내에서 생성 된 인스턴스를 반환해도 무방합니다.)

+0

입력 해 주셔서 감사합니다.하지만 여전히 줄 반환 오류에 대한 오류를 표시합니다. votesPerPosition; 클래스를 public으로 변경 한 후에도 String [] Vote() 오류가 진술이 아님을 나타냅니다. – SandyBites

+0

사과드립니다. 나는 대답 한 나의 대답에 오류가 있었다. 'return votesPerPosition;'이어야합니다. – Bathsheba

0

는 시도

public class Vote{ 
    int voter = 0; 
    Scanner sc; 
    BufferedReader br; 
    String line; 
    FileInputStream fis; 
    public int j = 0; 
    private int voterCount = 0; 
    public Arrays VotesPosition[]; 



    public String[] Vote() { 
     sc = new Scanner(System.in); 
     System.out.println("Enter file name > "); 
     String filename = sc.next(); 
     try { 
      br = new BufferedReader(new InputStreamReader(new FileInputStream(filename))); 
     } catch (FileNotFoundException ex) { 
      //Logger.getLogger(Vote.class.getName()).log(Level.SEVERE, null, ex); 
     } 
     do{ 
     voter++; 
      try { 
       filename = br.readLine(); 
      } catch (IOException ex) { 
       Logger.getLogger(Vote.class.getName()).log(Level.SEVERE, null, ex); 
      } 
      String votesPerPosition[] = filename.split(","); 
      for(String vote: votesPerPosition){ 
       Integer.parseInt(vote); 
       for (j = 0; j < votesPerPosition.length; j++);{ 
       System.out.println("voter " + voter + "voted for candidate #" + vote); 
       voterCount++; 
      } 
      } 
      return votesPerPosition; //<-------------- This is what I am trying//     
     }while(filename != null); 

    } 

    /** 
    * @return the voterCount 
    */ 
    public int getVoterCount() { 
     return voterCount; 
    } 

    /** 
    * @param voterCount the voterCount to set 
    */ 
    public void setVoterCount(int voterCount) { 
     this.voterCount = voterCount; 
    } 

    /** 
    * @return the VotesPosition 
    */ 
    public Arrays[] getVotesPosition() { 
     return VotesPosition; 
    } 

    /** 
    * @param VotesPosition the VotesPosition to set 
    */ 
    public void setVotesPosition(Arrays[] VotesPosition) { 
     this.VotesPosition = VotesPosition; 
    } 

    } 
0

: 그 방법은 다음과 같은 것을 할 수있다, 해시 테이블에

변경이 다음과 같은

public Hashtable VotesPosition = new Hashtable(); 

과 기능을 조금 구현을 변경하는 방법에 대한.

public void Vote() { 
     String filename = null, line = null; 
     sc = new Scanner(System.in); 
     System.out.println("Enter file name > "); 
     filename = sc.next(); 
     try { 
      br = new BufferedReader(new InputStreamReader(new FileInputStream(filename))); 
     } catch (FileNotFoundException ex) { 
      //Logger.getLogger(Vote.class.getName()).log(Level.SEVERE, null, ex); 
     } 
     if (br != null) { 
      do { 
       voter++; 
       try { 
        line = br.readLine(); 

        if (line != null && line.contains(",")) { 
         String votesPerPosition[] = line.split(","); 

         VotesPosition.put(voter, votesPerPosition.length); 
        } else if (line != null) { 
         VotesPosition.put(voter, 1); 
        } 
       } catch (IOException ex) { 
        line = null; 
        // Logger.getLogger(Vote.class.getName()).log(Level.SEVERE, null, ex); 
       } 
      } while (line != null); 
     } 
    }