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

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
Errors when the compiler is trying to build a custom JSP tag based on an Java Bean

Categories

HOME
pdf
listview
gerrit
angular-material
maven-3
mvvmcross
wamp
flyway4
append
synchronization
tomcat6
fancybox-3
offline
decimal
invantive-sql
excel-vba-mac
functional-testing
opennlp
javacv
react-css-modules
nhapi
maxmind
minitab
webkitspeechrecognition
tinymce-4
key-value-observing
xquery-3.0
caret
picasso
plsql-psp
suricata
git-merge
space-complexity
starteam
twitch
lumberjack
janrain
midl
netcdf4
apple-news
avconv
smartcontracts
datastax-startup
recursive-datastructures
sqlclient
nodebb
namecoin
pillow
deadbolt-2
probability-density
slicknav
merge-conflict-resolution
python-stackless
livequery
kendonumerictextbox
django-unittest
iis-arr
angular-local-storage
lustre
umbraco6
nsmutabledictionary
typekit
ng-animate
pick
winddk
phalanger
javafx-webengine
id3v2
xojo
reactfx
picturefill
stxxl
intentservice
ms-project-server-2010
buster.js
django-nonrel
pushbackinputstream
sharp-repository
clipper
cisco-jtapi
venn-diagram
javaspaces
netbeans-6.9
floating
.nettiers
sproutcore-2
modelstate
memory-size
libs
ctp4
signal-handling
lzh

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