Return value?
Hi there,
What I need is to write the output of another method to a notepad.
/**
* Creates or adds to a file of questions from details given by the user.
* It first asks for the name of the file to be created or added to. It
* uses the helper method makeQuestion to get a question from the user.
* The details for each question (the question, the possible answers and the
* number of the correct answer) are appended to the file using csv format.
*/
publicvoid run()
{
String nl = System.getProperty("line.separator");
OUDialog.alert("This allows you to add questions to an existing test file" + nl
+" or to a new test file. First you need to choose or name the file.");
String pathname = OUFileChooser.getFilename();
File aFile =new File(pathname);
BufferedWriter bufferedFileWriter =null;
try
{
bufferedFileWriter =new BufferedWriter(new FileWriter(aFile));
Question nextQuestion = this.makeQuestion();
String options[];
while (nextQuestion !=null)
{
/**
* The code needs to go here, write the output from makeQuestion()?
*/
bufferedFileWriter.newLine();
nextQuestion = this.makeQuestion();
}
}
catch (Exception anException)
{
System.out.println("Error: " + anException);
}
finally
{
try
{
bufferedFileWriter.close();
}
catch (Exception anException)
{
System.out.println("Error: " + anException);
}
}
}
/**
* Inputs the details of a question from the user. It uses these details to
* create a Question object which it returns. It uses the constant
* NUMBER_OF_QUESTIONS defined in Quiz.
*/
private Question makeQuestion()
{
String theQuestion = OUDialog.request("Enter the question or * to finish","");
if (theQuestion.equals("*"))
{
returnnull;
}
String[] options =new String[Quiz.NUMBER_OF_OPTIONS];
for (int i = 0; i < options.length; i++)
{
options[i] = OUDialog.request("Enter answer " + (i + 1) +": ","");
}
int theAnswer = Integer.parseInt(OUDialog.request(
"Enter number of the correct answer.",""));
returnnew Question(theQuestion, options, theAnswer);
}
I,m not sure how to write the code which would allow me to get the output from makeQuestion method and put it into the run method so writtig it into the notepad file chosen. I have wriitten were this code needs to go.
Hope someone can help.
Jon
Yah! Sorry about that, bad day with children and the other 1000 jobs I'd been left to do.
Right, what happens is that I run method run() and within this method is a helper method. This method makeQuestion() this method asks you if you want to add a question to the game.When the run() method is executed it asks you to choose a file to save to and that what I ment by 'notepad' so save to "test_file1.txt then the makeQuestion() method runs.
This method uses request boxes to ask you the question you want to enter into the game then it asks for four possible answers and finially asks you to enter the currect answer number. So if the question you enter into the first dialog box is "What is 10 muliplied 10?" the four possible answers you enter into the other dialog boxes could be 10, 100, 1000, 110 and finally in the last dialog box you input the correct answer position, in our case it would be 2.
Then some how put this inforation, which is stored inreturn new Question(theQuestion, options, theAnswer);
.So theQuestion is the one you typed in, then options is a string array containing the four answers and theAnswer, which is a int value of the correct position of the answer and that's when the run() method writes the information to the test_file1.txt. So when you open text_file1.txt there would be printed:
What is 10 multiplied by 10?, 10, 100, 1000, 110, 2. I now the buffer writes the information but I can't get the correct argument to put in thebufferedFileWriter.write(?)
to get the information from makeQuestion() method to acheive this. Once again sorry about that and hope this makes things a bit clearer.
Jon
And here is the Question class:public class Question
{
private String question; // the actual question
private String[] options; // the possible answers
private int answer; // the number of the correct answer (1, 2 , 3 or 4)
/**
* The constructor has three arguments: the actual question, an array
* containing the possible answers and the number of the correct answer.
*/
public Question(String theQuestion, String[] someAnswers, int theAnswer)
{
super();
this.question = theQuestion;
this.options = someAnswers;
this.answer = theAnswer;
}
/**
* Returns a string containing the question and the possible answers
* (numbered from 1) suitably formatted as a string for use in a dialog box.
*/
public String getFullQuestion()
{
String nl = System.getProperty("line.separator");
String fullQuestion = nl + this.getQuestion();
for (int i = 0; i < this.getOptions().length; i++)
{
fullQuestion = fullQuestion + nl + (i + 1) + ". " + this.getOptions()[i];
}
return fullQuestion + nl;
}
/**
* Returns the number of the correct answer.
*/
public int getAnswer()
{
return this.answer;
}
/**
* Returns the question only. (no answers)
*/
public String getQuestion()
{
return this.question;
}
/**
* Returns an array containing the possible answers.
*/
public String[] getOptions()
{
return this.options;
}
/**
* Might be useful for a quick check. It returns the question plus a reference
* to the array object containing the possible answers plus the correct answer
* number. Note that the array reference for each question ought to be different.
* If it isn't it means that a new array is not being created to hold the answers.
*/
public String toString()
{
return question + " " + options + " " + answer;
}
}