• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

handeling radio buttons

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67753
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67753
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
and heres my conversion of 0 & 1 to a boolean incase i made a mess of it :'(

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67753
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
    Sheriff
    Posts: 67753
    173
    Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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!
     
    Sheriff
    Posts: 22821
    132
    Eclipse IDE Spring Chrome Java Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Bear was talking about the error Manish Sridharan mentioned.
     
    Bear Bibeault
    Sheriff
    Posts: 67753
    173
    Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
    Sheriff
    Posts: 67753
    173
    Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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!
     
    Ranch Hand
    Posts: 115
    Eclipse IDE Tomcat Server Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    Police line, do not cross. Well, this tiny ad can go through:
    New web page for Paul's Rocket Mass Heaters movies
    https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
    reply
      Bookmark Topic Watch Topic
    • New Topic