adding data to objects within ArrayList
I am trying to create an ArrayList based on Questions. All the data is stored within a text file, and I am trying to use StingTokenizer to break up the pieces of information so I can input it into each Question object.
Question q1, q2, q3, q4, q5;
ArrayList<Question> quizbank =new ArrayList<Question> ();
try
{
while((oneLine = fileHandle.readLine()) !=null)
{
StringTokenizer st =new StringTokenizer(oneLine,",");
questionTemp = st.nextToken();
answerTemp = st.nextToken();
int complexityTemp = Integer.parseInt(st.nextToken());
q? =new Question(questionTemp, answerTemp, complexityTemp);
}
fileHandle.close();
}
My question is how can I loop through my q1, q2, q3, etc so I can create teh appropriate Question object while I read each line of the input file. If I can not achieve this easily, are there any suggestions on routes I can take to do something similar to this? Thanks in advance.
[1370 byte] By [
Triple_Ga] at [2007-10-2 5:06:19]

ArrayList<Question> quizbank = new ArrayList<Question> ();
try
{
while ((oneLine = fileHandle.readLine()) != null)
{
StringTokenizer st = new StringTokenizer(oneLine, ",");
String questionTemp = st.nextToken();
String answerTemp = st.nextToken();
int complexityTemp = Integer.parseInt(st.nextToken());
Question q = new Question(questionTemp, answerTemp, complexityTemp);
quizbank .add(q);
}
fileHandle.close();
}
I have done as you suggested, and it works. I can quizbank.size() to see that the appropriate number of objects have be entered. When I compile though, it gives me a NoSuchElementException. Is there any way to supress the error because I tried setting up a while loop checking whether st.HasMoreTokens(), but it wouldn't save to my variables correctly. Thank in advance.
Not sure what you mean by wouldn't save my variables correctly. Using the hasMoreTokens() method is the proper way of determing if the string tokenizer contains more tokens.
ArrayList<Question> quizbank = new ArrayList<Question>();
try
{
while((oneLine = fileHandle.readLine()) != null)
{
StringTokenizer st = new StringTokenizer(oneLine, ",");
//System.out.println(oneLine);
while(st.hasMoreTokens())
{
questionTemp = st.nextToken();
answerTemp = st.nextToken();
complexityTemp = Integer.parseInt(st.nextToken());
}
Question q = new Question(questionTemp, answerTemp, complexityTemp);
quizbank.add(q);
}
fileHandle.close();
}
catch(Exception e)
{
System.out.println(e);
}
Sorry about that, this is what I was meaning. I want to find a way to stop the Exception that I am getting. When I try to add the hasMoreTokens check, I am getting initalization errors on my Temp variables.
Those variables in your current code sample are not declared. However in your previous sample they were declared. You need to declare the variables you are using.
They are declared at the beginning of the main class...
FileReader theFile = new FileReader("quiz.txt");
BufferedReader fileHandle = new BufferedReader(theFile);
String oneLine, questionTemp, answerTemp;
int complexityTemp;
Simply initialize those variables where they are declared.
You can use the quizbank ArrayList. Or, you can use a HashMap with String "q1", "q2", "q3", etc. (or whatever) as keys and Question objects as values. See example with JTextFields in the following thread: http://forum.java.sun.com/thread.jspa?threadID=634815
In your code in Reply #4, you call hasMoreTokens once, then read three tokens. If you don't have at least three tokens, you will still get NoSuchElementException. You should call hasMoreTokens before every nextToken to avoid that.
I was thinking of using the HashMap, but my requirements are you use an array. While I am at it, when I am using the Question q = new Question(...), how are the variables named while the loop is being executed? I don't see how this is happening.
I noticed that my text file had spaces after the last line of text. Once I removed the extra spaces, the exception went away even without using the hasMoreTokens().
> I was thinking of using the HashMap, but my
> requirements are you use an array. While I am at it,
> when I am using the Question q = new Question(...),
> how are the variables named while the loop is being
> executed? I don't see how this is happening.
Which variables? You can now get your questions as:
quizbank.get(0);
quizbank.get(1);
...
That is what I noticed, but I mean what was the significance of re-creating a Question q? I thought you could only declare an object once.
You declare the variable q once. You create a different object every time you run through the loop. q is a reference to an object. It will get a different value every time (even if you declare q before the loop, and just reassign it in the loop). The objects (instances of Question) are on the heap. If you look at your quizbank, you will see that you do have distinct Question objects. The "quizbank.add(q);" adds a reference to the ArrayList, and "quizbank.get(0);" returns a reference.
MLRona at 2007-7-20 18:28:45 >

Thank you for the explanation. I think I get it now.
i am trying to display the data from objects that are stored in array list.
for example: if my data is stored at runtime.....in object & that objects are stored in arraylist.
if i am storing books information in objects, and adding that object to array list but when i want to display the last object data is displaying....
could it be possible for u to answer my questain.
Thanks..
Hi just a small change.Do it this way
// Commented Code Question q1, q2, q3, q4, q5;ArrayList<Question> quizbank = new ArrayList<Question> ();
try
{
while((oneLine = fileHandle.readLine()) != null)
{
StringTokenizer st = new StringTokenizer(oneLine, ",");
questionTemp = st.nextToken();
answerTemp = st.nextToken();
int complexityTemp = Integer.parseInt(st.nextToken());
Question q = new Question(questionTemp, answerTemp, complexityTemp);
quizbank.add(q);}
fileHandle.close();
}