?
Solved

Converting int to String

Posted on 2003-11-06
17
Medium Priority
?
52,508 Views
Last Modified: 2011-08-18
How to convert an integer value to string?

// for string to integer we use
string s = "1";
int i = Integer.parseInt(s);

//what code should b used to convert an int, with the value given below, to string

int i=1;
...????

In ASP we use CStr for that purpose, what is it's alternate used here in JSP... It's urgent
0
Comment
Question by:adnan_rais
  • 6
  • 6
  • 2
  • +3
17 Comments
 
LVL 18

Expert Comment

by:bobbit31
ID: 9697777
String s = new Integer(i).toString();

OR

String s = "" + i;


0
 
LVL 6

Expert Comment

by:kotan
ID: 9697890
Integer.toString(i);
0
 

Author Comment

by:adnan_rais
ID: 9698060
The source code is as follows... none of the solutions defined by kotan and bobbit31 worked properly as the program returns some error regarding to NullPointerException. Remember I'm using JBuilder 7 as the development tool n can't b changed due to the need of my assignment. So what's the correct solution......??? It's urgent

   <select name="day">
      <option value="none">Day</option>
<%
  String mday;
  for(int i=1; i<=31; i++)
   {
     mday = new Integer(i).toString();
     if(mday.equals(request.getParameter("day"))){
       out.println("<option selected>" + mday + "</option>");
     }else
     out.println("<option value=" + i + ">" + i + "</option>");
   }
%>
   </select>
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 18

Accepted Solution

by:
bobbit31 earned 80 total points
ID: 9698185
make sure request.getParameter("day") is not null!

you can do this as well:

String day = request.getParameter("day");

if(day != null && mday.equals(day)) {
       out.println("<option selected>" + mday + "</option>");
     }else
     out.println("<option value=" + i + ">" + i + "</option>");
   }
0
 

Author Comment

by:adnan_rais
ID: 9698202
The program still responded with errors
0
 
LVL 28

Expert Comment

by:rrz
ID: 9698205
which line is the problem?  
maybe this one?
>   if(mday.equals(request.getParameter("day"))){  
If browser didn't send  "day" then  NPE would occur.
0
 
LVL 28

Expert Comment

by:rrz
ID: 9698220
Please show us the error message and tell us which line is problem.
0
 

Author Comment

by:adnan_rais
ID: 9698236
The error doesn't point to any particular line... Here is the error code

Apache Tomcat/4.0.3 - HTTP Status 500 - Internal Server Errortype Exception reportmessage Internal Server Errordescription The server encountered an internal error (Internal Server Error) that prevented it from fulfilling this request.exception java.lang.NullPointerException
 at org.apache.jsp.CreateUser$jsp._jspService(CreateUser$jsp.java:118)
 at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:107)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
 at org.apache.jasper.servlet.JspServlet$JspServletWrapper.service(JspServlet.java:202)
 at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:382)
 at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:474)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
 at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:243)
 at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
 at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
 at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
 at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:190)
 at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
 at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
 at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
 at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2343)
 at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
 at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
 at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:170)
 at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
 at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:170)
 at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
 at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
 at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
 at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
 at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
 at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
 at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
 at org.apache.catalina.connector.http.HttpProcessor.process(HttpProcessor.java:1012)
 at org.apache.catalina.connector.http.HttpProcessor.run(HttpProcessor.java:1107)
 at java.lang.Thread.run(Thread.java:484)

Should there b any change to be made in the page directive by importing any class file
0
 

Author Comment

by:adnan_rais
ID: 9698257
<%@ page language="Java" import="java.sql.*,java.util.*,java.lang.*" %>
0
 
LVL 28

Expert Comment

by:rrz
ID: 9698258
Just an idea, instead of
>    <option value="none">Day</option>
use
<option value="none" selected="selected">Day</option>
0
 

Author Comment

by:adnan_rais
ID: 9698271
no rrz@871311, ur idea didn't help
0
 
LVL 28

Expert Comment

by:rrz
ID: 9698273
>java.lang.NullPointerException
 at org.apache.jsp.CreateUser$jsp._jspService(CreateUser$jsp.java:118)

Look in Tomcat's work directory for  CreateUser$jsp.java
0
 

Author Comment

by:adnan_rais
ID: 9698288
how to get into it?
0
 
LVL 28

Expert Comment

by:rrz
ID: 9698296
>Should there b any change to be made in the page directive by importing any class file    
No, that would not create a NPE.
0
 
LVL 28

Expert Comment

by:rrz
ID: 9698319
Shut down Tomcat to look at work folder.
0
 
LVL 1

Expert Comment

by:yapsing
ID: 9699154
i think sending something over is better solution. like:
http://url?day=something
0
 

Expert Comment

by:Morphuess
ID: 10403577
I've used this to convert int to String and it works for me.

String myString = new String ();
myString = String.valueOf(i);
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often times it's very very easy to extend a volume on a Linux instance in AWS, but impossible to shrink it. I wanted to contribute to the experts-exchange community a way of providing a procedure that works on an AWS instance. It can also be used on…
Applications for our next round of the Experts Exchange Scholarship Contest are starting to roll in. It made us wonder what our past winners are up to these days. Here's a look at what four winners experienced with the contest and what they're doing…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Loops Section Overview
Suggested Courses
Course of the Month13 days, 16 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question