jsp-tags


JSP Quiz process is not giving me the correct percentage scored


I have the following code quiz.jsp:
<form action= "execute.jsp" method="post" >
<table>
<%
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:mysql....);
Statement st=conn.createStatement();
ResultSet rs=st.executeQuery("Select * from questions");
int i=1;
while(rs.next()){
int in=rs.getInt(1);
%>
<tr>
<td>
<%=i%><p id="test"><%=rs.getString("questions")%></p>
</td>
</tr>
<tr id="two">
<td>
<input type="radio" value=<%=rs.getString("answerone")%> name="radio<%=in%>" id="a"/><%=rs.getString("answerone")%>
<input type="radio" value=<%=rs.getString("answertwo")%> name="radio<%=in%>" id="b"/><%=rs.getString("answertwo")%>
<input type="radio" value=<%=rs.getString("answerthree")%> name="radio<%=in%>" id="c"/><%=rs.getString("answerthree")%>
<input type="radio" value=<%=rs.getString("answerfour")%> name="radio<%=in%>" id="d"/><%=rs.getString("answerfour")%>
</td>
</tr>
<%
i++;
}
}catch(Exception ex){
out.print("Exception Caught"+ex);
}
%>
</table>
<div id="buttons">
<input type="submit" value="submit"></input>
</div>
</form>
</div>
execute.jsp
<%!
int calculatePercentage(int a,int b)
{
int result=(a/b) * 100;
return result;
}
%>
<%
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:mysql:......);
Statement st=conn.createStatement();
ResultSet rs=st.executeQuery("Select * from questions");
int j=0;
String[] correct=new String[4];
while(rs.next()){
j++;
correct[j]=rs.getString("iscorrect");
//out.println(correct[j]);
}
String check[]=new String[4];
for(int x=1; x<4; x++){
check[x]=request.getParameter("radio"+x);
//out.print(check[x]);
}
int count=0;
for(int a=0;a<correct.length;a++){
if((correct[a]!=null)&&(check[a]!=null)){
if(correct[a].equals(check[a])){
out.println(correct[a]);
out.println(check[a]);
count++;
}
}
}
int max=3;
int score=calculatePercentage(count,max);
out.println("Your "+score+" answers are correct");
// out.println("Your percentage scored is: "+score +"%");
}catch(Exception ex){
out.print("Exception Caught"+ex);
}
%>
Issue:
The problem I am having is that the test score is only accurate(100%) if all answers are correct.
if one of the radios is not select
if one or more is wrong
the result is 0%. I looks like the count is not passing the values correctly or the method is not executing well. Although if I do out.print(count), it displays the correct count.
Please advice.
Thanks

Related Links

Simple property not found?
Reusing JSP custom tags inside html
JSP Quiz process is not giving me the correct percentage scored
Refer the id variable of logic - iterate using jstl-el
Invalid tagdir attribute while web-fragment is used
JSP foreach tag for two variables
Adding Parameter to href tag before uri
Servlet exception stream closed jsp
creating and exporting a csv from Display tag table
bean write to number type id
restrict image file size in alloy UI
The function getContextPath must be used with a prefix when a default namespace is not specified
how can i retrive parameters from EncodeURL using jsp?
onclick tag giving error in h:commandLink
Displaytag - How to pass hidden value in url?
Embedding Tag Files in a JAR

Categories

HOME
ionic-framework
bluetooth
openmp
push-notification
path-finding
view
stock
fft
lodash
iot
fme
sd-card
q
framework7
routes
setup-deployment
google-oauth
packages
alpha
indesign
contact
basic
floating-action-button
correlation
remote-access
commonmark
percona
telephony
reactive-cocoa
sms-gateway
firefox-webextensions
google-pagespeed
plunker
object-detection
orleans
telerik-reporting
apache-commons-io
virtualdub
opentype
gsmcomm
r-raster
binary-data
iframe-resizer
twilio-api
xquery-3.0
textmate
y86
mapdb
jmonkeyengine
host
x11-forwarding
occlusion
lumberjack
elmah
google-closure
nand2tetris
chain-builder
pnotify
statsd
nativeapplication
slickedit
pg-dump
android-cursor
linode
mcafee
dwscript
minimization
yt-project
infix-notation
ado.net-entity-data-model
sts-springsourcetoolsuite
plottable.js
multiple-regression
dlna
yaws
ipconfig
energy
kendonumerictextbox
jms2
endeca-workbench
oberon
proj4js
nsmutabledictionary
operator-precedence
c3
device-manager
rdl
castle
yorick
rtmfp
resty-gwt
access-rights
sitemesh
ril
pvrtc
dsn
hosts-file
dbproviderfactories
asyncfileupload
stage
netbeans-7.1
disclosure
jmock
telerik-scheduler
lpeg
filtered-index
mirah
meego
geneva-server
signal-handling
document-conversion

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App