JSP Quiz page problem

[nobr]I am trying to create a quiz page where users are able to pick the correct choice for the correct answer.Everything works fine but i got 2 problems.

1) i tried this statement Just incase if a user does not select any option i still wanted to display the correct result

if(correctAnswers[i]!=usersAnswers[i] || usersAnswers[i]==null)//not working error

so right now my code works fine but as long the user selects the choice if either one of the choices are left blank and the user sumits the query i will get a jasper error which is this

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: null

org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:358)

org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)

org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)

javax.servlet.http.HttpServlet.service(HttpServlet.java:856)

root cause

java.lang.NumberFormatException: null

java.lang.Integer.parseInt(Integer.java:415)

java.lang.Integer.parseInt(Integer.java:497)

org.apache.jsp.QuestionForm_jsp._jspService(QuestionForm_jsp.java:88)

org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:133)

javax.servlet.http.HttpServlet.service(HttpServlet.java:856)

org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:311)

org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)

org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)

javax.servlet.http.HttpServlet.service(HttpServlet.java:856)

note The full stack trace of the root cause is available in the Tomcat logs.

2) i am trying to highlight the wrong answer with red and right answer with green i tried this code but not working

for(int i=0; i<correctAnswers.length; i++){

if(correctAnswers[i]==usersAnswers[i]){

out.println(">

");

out.println("option " + correctAnswers[i] +" is rite");

score = score + 1;

}

else{

out.println("

");

out.println("<span style=background-color: #FF0000>");

out.println(usersAnswers[i] +" is wrong correct answer is option " + correctAnswers[i]);

//out.println("</span>");

}

}

This span code is also not working. any clues? thanks

and here is my full code

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"

pageEncoding="ISO-8859-1"%>

<%@ pageimport ="java.util.Calendar" %>

<!DOCTYPE html PUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>Insert title here</title>

</head>

<body>

<b>Displaying The time</b>

<%

Calendar now =Calendar.getInstance();

int hour = now.get(Calendar.HOUR_OF_DAY);

int minute = now.get(Calendar.MINUTE);

if(hour<10)

out.println("0" + hour);

else

out.println(hour);

out.println(":");

if(minute<0)

out.println("0" + minute);

else

out.println(minute);

%>

<h2>Here are your answers</h2>

<br>

<%

int[] correctAnswers;

int[] usersAnswers;

int score;

correctAnswers =newint[7];

usersAnswers =newint[7];

score=0;

correctAnswers[0] = 2;

correctAnswers[1] = 1;

correctAnswers[2] = 1;

correctAnswers[3] = 1;

correctAnswers[4] = 3;

correctAnswers[5] = 2;

correctAnswers[6] = 1;

usersAnswers[0] = Integer.parseInt(request.getParameter("q1"));

usersAnswers[1] = Integer.parseInt(request.getParameter("q2"));

usersAnswers[2] = Integer.parseInt(request.getParameter("q3"));

usersAnswers[3] = Integer.parseInt(request.getParameter("q4"));

usersAnswers[4] = Integer.parseInt(request.getParameter("q5"));

usersAnswers[5] = Integer.parseInt(request.getParameter("q6"));

usersAnswers[6] = Integer.parseInt(request.getParameter("q7"));

for(int i=0; i<correctAnswers.length; i++){

if(correctAnswers[i]==usersAnswers[i]){

out.println("

");

out.println("option " + correctAnswers[i] +" is rite");

score = score + 1;

}

else{

out.println("

");

out.println("<span style=background-color: #FF0000>");

out.println(usersAnswers[i] +" is wrong correct answer is option " + correctAnswers[i]);

//out.println("</span>");

}

}

%>

You got <%=score %> correct out of <%=correctAnswers.length %>

</body>

</html>

Thanks for any help[/nobr]

[6915 byte] By [lrngjavaa] at [2007-11-27 10:32:45]
# 1

for part 1) -- when comparing objects you always want to check for null first, and only check for null if you are dealing with Objects (ie anything but ints, doubles, floats, booleans, chars....) Any class, including Strings, should be checked for null and then compared with the .equals(...) method. The == operator should only be used to compare primitives (ints, doubles, floats, bools, chars...)

den2681a at 2007-7-28 18:18:35 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

yes i know when comparing objects you had to use .equals method. my book taught me also in that manner. all the examples in jsp they have checked if the string is null then execute the if statement. so i am also doing the same thing

if(correctAnswers[i]!=usersAnswers[i] || usersAnswers==null)// still the jasper error

if(correctAnswers[i]!=usersAnswers[i] || usersAnswers[i]==null)//compile error the operator == is undefined.

i tried the equals method also still nothing

Message was edited by:

lrngjava

Message was edited by:

lrngjava

lrngjavaa at 2007-7-28 18:18:35 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

this is wrong, usersAnswers is an int[ ], so it can never contain a null as a value, thus the OR part of this expression is bogus.

if(correctAnswers[i]!=usersAnswers[i] || usersAnswers[i]==null)/

also, when making an int[ ] use the following notation, it's much easier to read this way.

int[] whatever = {2, 1, 5, 10, 20, 30 , 1, 2} ;

finally, the error you are getting is from your parseInt(...) calls, which means you are probably getting a null or empty string inside one of your "q#" variables.

den2681a at 2007-7-28 18:18:35 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

[nobr]> this is wrong, usersAnswers is an int[ ], so it can

> never contain a null as a value, thus the OR part of

> this expression is bogus.

>

> if(correctAnswers[i]!=usersAnswers[i] ||

> usersAnswers[i]==null)/

>

> also, when making an int[ ] use the following

> notation, it's much easier to read this way.

>

> int[] whatever = {2, 1, 5, 10, 20, 30 , 1, 2}

> ;

>

> finally, the error you are getting is from your

> parseInt(...) calls, which means you are probably

> getting a null or empty string inside one of your

> "q#" variables.

Thanks Master :--) i am new to this so trying to think of my own project and implementing it. sorry but string is great. Everything is working fine now :--). even my span is working fine now. if you answered wrong it will be highlited with red. if right then it will be highlighted with green.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"

pageEncoding="ISO-8859-1"%>

<%@ page import = "java.util.Calendar" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>Insert title here</title>

</head>

<body>

<b>Displaying The time</b>

<%

Calendar now =Calendar.getInstance();

int hour = now.get(Calendar.HOUR_OF_DAY);

int minute = now.get(Calendar.MINUTE);

if(hour<10)

out.println("0" + hour);

else

out.println(hour);

out.println(":");

if(minute<0)

out.println("0" + minute);

else

out.println(minute);

%>

<h2>Here are your answers</h2>

<br>

<%

String[] correctAnswers = {"2", "1", "1", "1", "3", "2", "1"};

String[] usersAnswers;

int score;

usersAnswers = new String[7];

score=0;

usersAnswers[0] = request.getParameter("q1");

usersAnswers[1] = request.getParameter("q2");

usersAnswers[2] = request.getParameter("q3");

usersAnswers[3] = request.getParameter("q4");

usersAnswers[4] = request.getParameter("q5");

usersAnswers[5] = request.getParameter("q6");

usersAnswers[6] = request.getParameter("q7");

for(int i=0; i<correctAnswers.length; i++) {

if(correctAnswers[i].equals(usersAnswers[i])) {

%>

<span style="background-color: #008000">

<%

out.println("

");

out.println("option " + correctAnswers[i] + " is rite");

score = score + 1;

}

else if(correctAnswers[i]!=usersAnswers[i] || usersAnswers[i]==null) {

%>

<span style="background-color: #FF0000">

<%

out.println("

");

//out.println("<span style=background-color: #FF0000>");

out.println(usersAnswers[i] + " is wrong correct answer is option " + correctAnswers[i]);

//out.println("</span>");

}

%>

</span>

<%

}

%>

You got <%=score %> correct out of <%=correctAnswers.length %>

</body>

</html>

[/nobr]

lrngjavaa at 2007-7-28 18:18:35 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

one more question say for instance i would like to upload my html page and my jsp page on my website. how can i do that? i mean do i have to check with my hosting people whether they support tomcat or not?

lrngjavaa at 2007-7-28 18:18:35 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

i have my website hosted from bluehost.com. talking with a technical staff i came to know that my hosting service does not provide tomcat server or to keep it more plain they does not support JSP pags? What the hell? now do i need to change my hosting services or is there any other way i can upload my projects on my website?

lrngjavaa at 2007-7-28 18:18:35 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7

you have to find a different host that supports .jsps.

you can try myjavaserver.com or you could host the site on your own using dynamic DNS (google it). it's a free service that will give you a small URL that will direct people to your home machine (or wherever you decide to host at).

den2681a at 2007-7-28 18:18:35 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...