2013-09-21 3 views
0

나는 파일을 가지고 arraylist에 넣는 중입니다. 그런 다음 String 클래스의 split 메소드를 사용하여 포맷 된 토큰을 사용하여 arraylist를 만듭니다. 내 테스터 클래스에서 그것은 나를 줄 또는 null에 넣어 수 있습니다. Null은 아무 것도 수행하지 않으며 3 개의 모든 필드에서 첫 번째 토큰을 입력합니다. 나에게 올바른 정보를주기 위해 while 루프를 사용할 수 없다. 내 클래스 생성자에서 찾고있는 것을 얻기 위해 무엇을 전달해야합니까? 여기 내 코드가있다.원하는 결과를 얻기 위해 어떤 정보를 전달해야합니까?

public class TriviaGame { 
    String category; 
    String question; 
    String answer; 




public TriviaGame(String category, String question, String answer) { 
    super(); 
    this.category = category; 
    this.question = question; 
    this.answer = answer; 
} 





@Override 
public String toString() { 
    return "TriviaGame [category=" + category + ", question=" + question 
      + ", answer=" + answer + "]"; 
} 





/** 
* @return the category 
*/ 
public String getCategory() { 
    return category; 
} 





/** 
* @param category the category to set 
*/ 
public void setCategory(String category) { 
    this.category = category; 
} 





/** 
* @return the question 
*/ 
public String getQuestion() { 
    return question; 
} 





/** 
* @param question the question to set 
*/ 
public void setQuestion(String question) { 
    this.question = question; 
} 





/** 
* @return the answer 
*/ 
public String getAnswer() { 
    return answer; 
} 





/** 
* @param answer the answer to set 
*/ 
public void setAnswer(String answer) { 
    this.answer = answer; 
} 





} 

import java.io.File; 
import java.io.IOException; 
import java.util.Scanner; 


public class TriviaGameTester2 { 

/** 
* @param args 
*/ 
public static void main(String[] args) throws IOException { 
    File dataFile = new File ("trivia.txt"); 

    Scanner infile = new Scanner(dataFile); 
     String line; 
     String [] words; 
     TriviaGame [] games; 
     final int NUM_QUESTIONS = 300; 
     int counter; 
     TriviaGame temp; 

     games = new TriviaGame[NUM_QUESTIONS]; 

     counter = 0; 


     while(infile.hasNext()){ 
      line = infile.nextLine(); 
      words = line.split("[,]"); 


      temp = new TriviaGame(null, null, null); 
      //what should I put here to get my categories, questions 
          //and answers? 
      games[counter] = temp; 
      counter++; 

     } 

     infile.close(); 

     for(int i = 0; i < counter; i++){ 
      System.out.println(games[i]); 
     } 


     } 



} 
+0

문제는 어느입니까? – ssantos

+0

내 gameTester2 클래스에서 temp =, 지금 당장 Null, null, null, Eclipse 권장 라인을 말합니다. null을 사용하면 category = entertainment, question = entertainment 및 answer = entertainment를 사용하면 category = null, question = null, answer = null을 얻습니다. 그런 다음 두 번째로 모두에 대한 질문을합니다. 모든 이에 대한 대답. 한 가지 질문, 하나의 대답, 하나의 대답을 얻을 수 있도록 내가 무엇을 가지고 있어야하는지 질문합니다. –

+0

파일의 형식은 무엇입니까? – MadProgrammer

답변

1

코드의 라인은 파일에 범주, 질문과 답변이 추가 된 순서에 따라 다음 테스터. 파일의 각 줄이라고 가정하면 : 카테고리, 질문, 대답, 코드가 될 것이다 :

temp.setCategory(words[0]); 
temp.setQuestion(words[1]); 
temp.setAnswer(words[2]); 

또는, 당신은 다만 수 :

temp = new TriviaGame(words[0], words[1], words[2]); 
+0

어디에서 null, null, null을 사용합니까? –

+0

그럴 수 있습니다. temp = 새로운 TriviaGame (words [0], words [1], words [2]); 처음 게시 한 코드는 null, null, null을 유지하려고한다고 가정했습니다. 그렇지 않으면이 코드가 완전히 대체 할 수 있습니다. –

+0

굉장, 작동합니다. 이제는 정말 좋은 물건으로 이동할 수 있습니다! 감사 –

관련 문제