how to send a String to a constructor in another class
simplifying my problem:
I am trying to send a String that the user has provided which has two parts (query and result) to the Question constructor IN ANOTHER CLASS to create a Question object where i can assign the object into an array. i have two classes created Question and Quiz. the Question constructor looks like this:
//--
// Constructor: Sets up the question with a default complexity.
//--
public Question (String query, String result)
{
question = query;
answer = result;
complexityLevel = 1;
}
what do i need to change in the constructor so i can do what i need to do and what needs to go in Quiz class to send the strings into this constructor and then the object into an array?
> simplifying my problem:
>
> I am trying to send a String that the user has
> provided which has two parts (query and result) to
> the Question constructor IN ANOTHER CLASS
This makes no sense. The Question c'tor is in the Question class.
Do you mean these strings originate in one class, and then you're going go call a method in another class, and that class will invoke the Question c'tor? If so, then just make the method take those same two strings.
If that's not what you mean, please clarify. Post code that show's what you're trying to do, and if part of it doesn't compile because that's that part you don't understand, that's okay.
jverda at 2007-7-11 15:52:35 >

yeah i thought that might have sounded confusing just don't know how to say it. anyway with the constructor above in class Question needs to be passed strings from class Quiz and here is my code i tried to do it with:
public class Quiz
{
Question[] array = new Question[25];
public void addQuestion(String qTemp)
{
Scanner scan = new Scanner(System.in);
System.out.println ("Type the question you want to add followed by comma and the answer");
for (int i = 0; i < array.length; i++)
{
qTemp = scan.nextLine();
}
for (int x = 0 ; x < array.length ; x++)
qTemp.add(array[x]); // errors here
}
my professor responded with this: "A question object is not the same thing as a String. I do not have a copy of the Question class with me, but you will need to send the String the user provided for the question and answer to the Question constructor to actually create a Question object, which can then be assigned into the Question array."
Message was edited by:
dalearyous
You have multiple problems in your code.
First and foremost: declaring an array does not create any objects in the array, it just creates an empty array.
Your addQuestion method doesn't make any sense. You pass in a qTemp String variable which then gets overwritten 25 times, meaning at the end of the for loop it contains only the final line of input.
Then you try to somehow add Questions from the array to the qTemp string, which (obviously) doesn't work.
My guess is, you need to get the input somewhere else (perhaps in the main method of your program) and then call addQuestion() for each line of input you get.
Inside addQuestion() you will need to split the input String into two parts, the question and the answer. Those two String can subsequently be used to construct a new Question. This Question object can then be added to the array of questions.