Im working on a quiz servlet, where a question is retrieved from a database and then the user must choose true or false, I have to implement it with radio buttons but I'm having difficulty getting the buttons to display the correct response.It just seems to always say that the answer is correct.Here is some of the code that should illustrate the problem a bit better.
Here is the form part of my main servlet, which sends the data to a checkanswer servlet
and here is my handeling of the form in check answer
Any help with this would be much appreciated....
Secondly, where is it going wrong? At what point is it doing something different than what you expect?
If you're not set up in a debugger, add some logging statements to show that the value of the variables are at various stages so you can track down where it deviates from the expected.
in particular these lines, is the value 0 & 1 acceptable?i am converting them to boolean....
http://localhost:8080/quiz/checkanswer?radiochoice=1 for true and http://localhost:8080/quiz/checkanswer?radiochoice=0 for false...would my problems stem from my choice of doPost?
A couple of other points:
Bear Bibeault wrote:If you had added debug statements like I hinted at, you would have found the assignment error noted above.
A couple of other points:
Your form is generating a GET. A doPost() will not be called unless you specifically call it from doGet() (which is bad paractice). Match the method to the form. Never re-invent the wheel. Look on the Boolean class to see if there's a conversion that you can use instread of writing your own. Once you find the above, it will tell whether 0 and 1 are good choice for the values.
Thanks bear, but im only learning, so i wouldnt be to good on the uptake of hints!I did look into the boolean conversion but it appeared as far as I could tell that I had to make the conversion myself.
What do you men by debug statements? what i did was just added a print out of values so on submission i was getting
"Congratulations, you answered "true" and the answer was "true" so you are correct" in both instances.and I am still completley baffled by the result
Brendan Cregan wrote:Thanks bear, but im only learning
Then you're in the right place. Here at the Ranch you won't just be given answers, but guided into finding them yourself, learning much in the process.
I did look into the boolean conversion but it appeared as far as I could tell that I had to make the conversion myself.
Look harder. Inspect each method of Boolean to see if there's a method that will convert a string value into Boolean.
What do you men by debug statements? what i did was just added a print out
At the minimum, System.print.out will do. For more heavy-duty logging check out java.util.logging or Log4J.
Put such statements everywhere that you want to know what a value is. Is the value gotten from the param correct? Is the value from the DB correct? Dose the comparison do what you expected? And so on.
You did fix your assignment syntax error, right?
Bear Bibeault wrote:
Brendan Cregan wrote:i was confused with the assignment since it worked for !=
That's not an assignment, that's just another of the comparison operators: == != <= >=
The = operator is assignment, not comparison.
Thanks for clearing that up for me Bear!and thanks for the help!