0

I want to add database elements into array. how do I do that.

the table row has elements a to d.

i want to do this because. I have created a quiz and each time the user answers a question the answer in stored in a database. the questions are multiple choice. so a, b, c or d. (there are 5 questions) i am creating a servlet score page. so once they are in an array the i can check each element.

here is my code

private String ans[];
...  
int i = 0;

while (rs.next()) {
ans[i] = {rs.getString(1)};
i++;
}
0

3 Answers 3

3

Consider using an ArrayList<String> instead.

private ArrayList<String> ans = new ArrayList<String>();
...

while (rs.next()) {
    ans.add(rs.getString(1));
}
Sign up to request clarification or add additional context in comments.

1 Comment

beat me by like 5 seconds, but depending on the size a LinkedList will be more effecient.
1

this will help you

private String ans[];
...  
int i = 0;

while (rs.next()) {
ans[i] = rs.getString(1);
i++;
}

Comments

0
private LinkedList<String> ans = new LinkedList<String>();
//...  

while (rs.next()) {
ans.add(rs.getString(1));
}

This would work better as you can have multiple elements.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.