나는 파일을 가지고 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]);
}
}
}
문제는 어느입니까? – ssantos
내 gameTester2 클래스에서 temp =, 지금 당장 Null, null, null, Eclipse 권장 라인을 말합니다. null을 사용하면 category = entertainment, question = entertainment 및 answer = entertainment를 사용하면 category = null, question = null, answer = null을 얻습니다. 그런 다음 두 번째로 모두에 대한 질문을합니다. 모든 이에 대한 대답. 한 가지 질문, 하나의 대답, 하나의 대답을 얻을 수 있도록 내가 무엇을 가지고 있어야하는지 질문합니다. –
파일의 형식은 무엇입니까? – MadProgrammer