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

Youtube onYouTubePlayerReady() function not firing
username and password cannot be resolved or is not a field
How page directive “extend” in jsp works?
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?

Categories

HOME
winforms
pdf
jdo
hook
blogger
mockito
hashmap
platform-builder
electron
bookshelf.js
google-docs
getelementsbytagname
rdf
callback
baqend
enterprise-library-5
wamp
cloudkit
communication
dax
rascal
export-to-csv
nstableview
task
windows-server-2012
percona
ghc
angular-ui
interop
jqwidget
shared-hosting
immutable.js
ml
blazemeter
javacv
chromebook
kannel
spring-mybatis
minitab
facebook-apps
react-chartjs
column-family
espeak
dynamics-crm-2013
ansible-playbook
splice
angularjs-factory
elasticsearch-plugin
dism
pdf-reactor
firebase-admin
keydown
user-accounts
flickr-api
rainbowtable
network-flow
apache-fop
jedis
brightcove
jxcore
nodebb
android-cursor
arrow-keys
spring-cache
minimization
parallel-data-warehouse
visual-c++-2008
deadbolt-2
passport-google-oauth
thrust
fputcsv
actionbardrawertoggle
ptrace
two-factor-authentication
photobucket
rtbkit
thredds
libressl
browser-bugs
android-listview
ionic
cakephp-3.1
processmodel
p2
dia
website-monitoring
block-device
offloading
operator-precedence
android-imagebutton
id3v2
client-side-templating
fmod
tidy
blending
dbconnection
flash-builder4.5
mylyn
html-editor
referrer
browser-detection
cascalog
javaspaces
enter
netbeans-6.9
code-cleanup
doh
heartbeat
xdomainrequest
horizontal-accordion
javax.script
yui-datatable
datareader
dmx512
mirah
photoshop-cs4
data-driven
virtual-functions
swing-app-framework
eqatec
xetex
geneva-server

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