Results 1 to 4 of 4
  1. #1
    tahir90 is offline Member
    Join Date
    Dec 2009
    Posts
    2
    Rep Power
    0

    Unhappy how to convert string to int in guess number game in jsp

    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)





    --------------------------------------------------------------------------------

    I will be thankful if anyone can provide me the solution of this code or can edit my code correctly.. waiting for replies thx in advance..!!

  2. #2
    gcampton Guest

    Default

    int a;
    String b="69";

    a = Integer.parseInt(b);

    the variables don't need inverted commas, only string literals do

  3. #3
    tahir90 is offline Member
    Join Date
    Dec 2009
    Posts
    2
    Rep Power
    0

    Red face

    thx for the help but still iam having errors i have edited my code by makin 2 pages one is number.jsp and other is check.jsp and the code is as follows :

    number.jsp :

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


    check.jsp :

    <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)

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I know some Java but know no JSP, but I think that gcampton was suggesting that you change this:
    Java Code:
    10: <%
    11: int a=63;
    12: String n = request.getParameter("n");
    13: int g=Integer.parseInt("n");
    14:
    to this:
    Java Code:
    10: <%
    11: int a=63;
    12: String n = request.getParameter("n");
    13: int g=Integer.parseInt(n); // no quotes here
    14:
    The n variable (a terribly non-descriptive variable name, btw) holds a number String, and you want to parse this string via parseInt(n). If you try parseInt("n") you're trying to parse a letter, not a variable, and unless you're parsing hex numbers (by calling an overload of the parseInt method and setting the method's radix) and your letters are less than f, this makes no sense.

Similar Threads

  1. convert number to string
    By wizard_oz in forum New To Java
    Replies: 9
    Last Post: 11-16-2009, 08:04 PM
  2. how to convert String number to int
    By gabriel in forum New To Java
    Replies: 5
    Last Post: 08-02-2009, 04:46 PM
  3. Convert bar code to number
    By samatha_kudi in forum Advanced Java
    Replies: 1
    Last Post: 07-17-2009, 03:56 PM
  4. guess number game
    By mistah in forum New To Java
    Replies: 10
    Last Post: 11-23-2008, 04:37 AM
  5. sample of guess high and low game
    By pouria62 in forum AWT / Swing
    Replies: 1
    Last Post: 10-26-2008, 01:57 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •