plzz help me..!!!

tahir90's Avatar, Join Date: Dec 2009
Light Poster
Iam new to jsp and trying to get input from user and then check either the input match with the number provided by the programmer. here is my code :


<form method="get">
Guess number :
<input type="text" name="n" />

<%
String a=63;
String n = request.getParameter("n");
int g=Integer.parseInt("n");
if(g==a)
{
out.println("you won");
}
else
{
out.println("wrong guess");
}

%>
</form>


but i am having errors which are as follows :

type Exception report

message

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

exception

org.apache.jasper.JasperException: Unable to compile class for JSP:

An error occurred at line: 17 in the jsp file: /guess number.jsp
Type mismatch: cannot convert from int to String
14: <input type="text" name="n" />
15:
16: <%
17: String a=63;
18: String n = request.getParameter("n");
19: int g=Integer.parseInt("n");
20: if(g==a)


An error occurred at line: 20 in the jsp file: /guess number.jsp
Incompatible operand types int and String
17: String a=63;
18: String n = request.getParameter("n");
19: int g=Integer.parseInt("n");
20: if(g==a)
21: {
22: out.println("you won");
23: }


Stacktrace:
org.apache.jasper.compiler.DefaultErrorHandler.jav acError(DefaultErrorHandler.java:92)
org.apache.jasper.compiler.ErrorDispatcher.javacEr ror(ErrorDispatcher.java:330)
org.apache.jasper.compiler.JDTCompiler.generateCla ss(JDTCompiler.java:439)
org.apache.jasper.compiler.Compiler.compile(Compil er.java:334)
org.apache.jasper.compiler.Compiler.compile(Compil er.java:312)
org.apache.jasper.compiler.Compiler.compile(Compil er.java:299)
org.apache.jasper.JspCompilationContext.compile(Js pCompilationContext.java:586)
org.apache.jasper.servlet.JspServletWrapper.servic e(JspServletWrapper.java:317)
org.apache.jasper.servlet.JspServlet.serviceJspFil e(JspServlet.java:342)
org.apache.jasper.servlet.JspServlet.service(JspSe rvlet.java:267)
javax.servlet.http.HttpServlet.service(HttpServlet .java:717)
0
techgeek.in's Avatar, Join Date: Dec 2009
Skilled contributor
Hi tahir,
the way u developed ur code clearly indicates that u r new to JSP. Actually, u dnt have the clear idea about the get and post commands. When u use "get" or "Post" command, it indicates that the parameters are to be diverted to the some other page i.e in ur case the number which u r taking as an input from a end user must be carried to some other page nd in the other page the logic must be applied with the received input.
Let me explain u. Make one .html file and a .jsp file.
Name of the html file wud be number.html
nd that of the .jsp file be check.html

Now the snippet for html file wud be:-
HTML Code:
<html>
<body>
<form method="get" action="check.jsp">
<input type="text" name="n" />
<input type="submit"/>
</form>
</body>
</html>

NOw the snippet for the jsp page wud be:-

<html>
<body>
<%
String a=63;
String n = request.getParameter("n");
int g=Integer.parseInt("n");
if(g==a)
{
out.println("you won");
}
else
{
out.println("wrong guess");
}

%>
</body>
</html>
I hope you have understood the code.

Last edited by shabbir; 20Dec2009 at 19:10.. Reason: Code blocks
0
techgeek.in's Avatar, Join Date: Dec 2009
Skilled contributor
sorry!! u did one more thing wrong over dr...instead of
String a=63 u need to write
int a=63.. i overlooked that..sorry one again
0
tahir90's Avatar, Join Date: Dec 2009
Light Poster
Quote:
Originally Posted by techgeek.in View Post
Hi tahir,
the way u developed ur code clearly indicates that u r new to JSP. Actually, u dnt have the clear idea about the get and post commands. When u use "get" or "Post" command, it indicates that the parameters are to be diverted to the some other page i.e in ur case the number which u r taking as an input from a end user must be carried to some other page nd in the other page the logic must be applied with the received input.
Let me explain u. Make one .html file and a .jsp file.
Name of the html file wud be number.html
nd that of the .jsp file be check.html

Now the snippet for html file wud be:-
HTML Code:
<html>
<body>
<form method="get" action="check.jsp">
<input type="text" name="n" />
<input type="submit"/>
</form>
</body>
</html>

NOw the snippet for the jsp page wud be:-

<html>
<body>
<%
String a=63;
String n = request.getParameter("n");
int g=Integer.parseInt("n");
if(g==a)
{
out.println("you won");
}
else
{
out.println("wrong guess");
}

%>
</body>
</html>
I hope you have understood the code.
thx for the help but still iam having errors i have edited my code as follows :

number :

<form method="get" action="check.jsp">
<input type="text" name="n" />
<input type="submit"/>
</form>


check :

<body>
<%
int a=63;
String n = request.getParameter("n");
int g=Integer.parseInt("n");

if(g==a)
{
out.println("you won");
}
else
{
out.println("wrong guess");
}

%>
</body>

now the errors are as follows :

type Exception report

message

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

exception

org.apache.jasper.JasperException: An exception occurred processing JSP page /check.jsp at line 13

10: <%
11: int a=63;
12: String n = request.getParameter("n");
13: int g=Integer.parseInt("n");
14:
15: if(g==a)
16: {


Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handle JspException(JspServletWrapper.java:505)
org.apache.jasper.servlet.JspServletWrapper.servic e(JspServletWrapper.java:416)
org.apache.jasper.servlet.JspServlet.serviceJspFil e(JspServlet.java:342)
org.apache.jasper.servlet.JspServlet.service(JspSe rvlet.java:267)
javax.servlet.http.HttpServlet.service(HttpServlet .java:717)


root cause

java.lang.NumberFormatException: For input string: "n"
java.lang.NumberFormatException.forInputString(Unk nown Source)
java.lang.Integer.parseInt(Unknown Source)
java.lang.Integer.parseInt(Unknown Source)
org.apache.jsp.check_jsp._jspService(check_jsp.jav a:66)
org.apache.jasper.runtime.HttpJspBase.service(Http JspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet .java:717)
org.apache.jasper.servlet.JspServletWrapper.servic e(JspServletWrapper.java:374)
org.apache.jasper.servlet.JspServlet.serviceJspFil e(JspServlet.java:342)
org.apache.jasper.servlet.JspServlet.service(JspSe rvlet.java:267)
javax.servlet.http.HttpServlet.service(HttpServlet .java:717)
0
techgeek.in's Avatar, Join Date: Dec 2009
Skilled contributor
use the following code for check.jsp. I have verified it myself.

<html>
<body>
<%
int a=63;
int g=Integer.parseInt(request.getParameter("n"));
if(g==a)
{
out.println("you won");
}
else
{
out.println("wrong guess");
}

%>
</body>
</html>
0
tahir90's Avatar, Join Date: Dec 2009
Light Poster
thx friend thank you very much it really works and also helps me to understand the syntax and helped to improve my concept thank you once again..!!
0
techgeek.in's Avatar, Join Date: Dec 2009
Skilled contributor
netime... techgeek.in is always on...
0
venami's Avatar, Join Date: Dec 2008
Ambitious contributor
The reason for getting the error the second time(line 13) is that the Integer.parseInt() needs a number string. But what tahir used was Integer.parseInt("n") instead of using like this: Integer.parseInt(n).

So,
Integer.parseInt("n") will take the value n as parameter, and,
Integer.parseInt(n) will take the value 63 as parameter.
0
techgeek.in's Avatar, Join Date: Dec 2009
Skilled contributor
yah..u r correct..i have corrected that only when second time i posted the code for check.jsp
0
venami's Avatar, Join Date: Dec 2008
Ambitious contributor
Yes I could understand that from your two posts instead of a single post. In order for tahir to understand the reason for the error, I posted with a little bit of explanation