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

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
Creating nested custom tag using jsp2 tag files
Unable to access session on JSP
Dealing with <html:options>
No tag “tableFacade” defined in tag library imported with prefix “jmesa”

Categories

HOME
maven
amazon-ec2
cookies
keras
openstack
c#-4.0
reserved
nullpointerexception
json.net
electron
relay
objectgears
alfresco
onelogin
networkx
android-youtube-api
webrequest
synchronization
dax
collectd
n-gram
remote-access
flask-wtforms
carthage
worldwind
facebook-page
google-static-maps
interop
sms-gateway
autocad-plugin
cloudhub
language-agnostic
cultureinfo
wijmo
trading
angular2-aot
h2db
xpages-ssjs
numerical-methods
vlsi
brunch
fgetcsv
entitlements
microsoft-chart-controls
media-queries
google-rich-snippets
oracle-fusion-middleware
ioio
create-table
gtrendsr
android-nestedscrollview
picasso
devextreme
document.write
angularjs-factory
ensembles
fedex
mime
pubmed
dds
python-c-api
logparser
elgg
kbuild
lift-json
epson
cudafy.net
nbconvert
titanium-android
url-pattern
spring-android
py2app
markojs
skype4py
master-slave
rhino-servicebus
iad
thredds
jqgrid-formatter
computer-algebra-systems
wyam
dukescript
operation
gwidgets
asp.net-dynamic-data
java-metro-framework
issuu
starcluster
valuechangelistener
seaside
dealloc
balanced-payments
centos5
geos
sitemesh
inbox
soundtouch
html-editor
smtp-auth
anonymous-methods
chuck
runas
multipage
doh
rfc1123
jmock
radcombobox
w3wp.exe
wise
revisions
jquery-ui-droppable
commodore
ajax-forms

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