Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

handeling radio buttons

 
Brendan Cregan
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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....

Brendan
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64970
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all why are you emitting HTML from a servlet rather than using a JSP?

Secondly, where is it going wrong? At what point is it doing something different than what you expect?
 
Brendan Cregan
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in the spec we are only able to use servlets...its going wrong when i submit my answer, it always says that it is the correct answer, i have a feeling it has something to do with my radio buttons but im not really sure as im not very familiar with HTML.


Thanks

Brendan
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64970
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What have you one to debug the issue? Are you getting the right value from the parameter? Are you getting the expected value from the DB? Is the comparison what is failing.

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.
 
Brendan Cregan
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, so i ran a little test and it turns out the servlet keeps thinking im answering true and doesnt ever seem to pick up when i pick false, I know it has something to do with the radio buttons...

in particular these lines, is the value 0 & 1 acceptable?i am converting them to boolean....


Thanks again


Brendan
 
Brendan Cregan
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
one more weird thing i noticed is that in the address bar it seems to be working ok...
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?


Thanks
 
Brendan Cregan
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and heres my conversion of 0 & 1 to a boolean incase i made a mess of it :'(

 
Manish Sridharan
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

In the below code you are not comparing the value of the variable instead assigning value of the answer variable to the variable myChoice.


Thanks,
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64970
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  •  
    Brendan Cregan
    Ranch Hand
    Posts: 35
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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

    Thanks

    Brendan
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Pie
    Posts: 64970
    86
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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?
     
    Brendan Cregan
    Ranch Hand
    Posts: 35
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    You did fix your assignment syntax error, right?

    i pressume this means that I am a complete fool for whatever error i have made!putting it in bold just makes your wisdom even more useful!
     
    Rob Spoor
    Sheriff
    Pie
    Posts: 20550
    57
    Chrome Eclipse IDE Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Bear was talking about the error Manish Sridharan mentioned.
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Pie
    Posts: 64970
    86
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Rob Spoor wrote:Bear was talking about the error Manish Sridharan mentioned.

    This.
     
    Brendan Cregan
    Ranch Hand
    Posts: 35
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Manish Sridharan wrote:Hi,

    In the below code you are not comparing the value of the variable instead assigning value of the answer variable to the variable myChoice.


    Thanks,


    Thanks Manish, i was confused with the assignment since it worked for !=

    Brendan
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Pie
    Posts: 64970
    86
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    Brendan Cregan
    Ranch Hand
    Posts: 35
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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!
     
    Singh Harmeet
    Ranch Hand
    Posts: 115
    Eclipse IDE Java Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    this assignement statement is work in if state because , the value is boolean, other wise it is compile time error.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic