How to convert String to int in JSP?
Hi,
I set a session attribute in Servlet and want use it in JSP, How can I convert it to int or Integer?
the line in my code doesn't work:
int quantity=(int)session.getAttribute("vehiclequantity") ;
Thanks in advance.
Wolf
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
<HTML>
<HEAD>
<TITLE>Using the for Statement</TITLE>
</HEAD>
<BODY>
<H1>Using the for Statement</H1>
<%=session.getAttribute("vehiclequantity") %>;
<%
int loopIndex;
int quantity=(int)session.getAttribute("vehiclequantity") ;
out.println(quantity);
for (loopIndex = 1; loopIndex <=2; loopIndex++) {
out.println("This is iteration number "
+ loopIndex + "<BR>");
%>
</BODY>
</HTML>
Learning how to read errors and understand them will help you solve your problems quicker by yourself... So lets take a look at the error and classes involved...
The error says:
"Cannor Resolve Symbol: method valueOf(java.lang.Object) in the class java.lang.Integer" and gives you line line where the error occurs: Integer quantity = Integer.valueOf(session.getAttribute("vehiclequantity"));
Now, if we look at the API for the Integer we notice that are are only two valueOf methods: valueOf(java.lang.String s) and valueOf(java.lang.String s, int radix). Not valueOf(java.lang.Object) method.
Now we look at the getAttribute(java.lang.String name) method of HttpSession we see that the method returns a java.lang.Object. Now, you know you put a java.lang.String into that attribute, but the get method returns an Object. This is because you could have put any object in there, an Integer, a String, or some other class instance. But you know it is a String, so you can cast the returned value to a String, so that you will be calling the valueOf(java.lang.String s) method of Integer with the Object returned from the HtttpSession's getAttribute(java.lang.String name) method:
Integer quantity = Integer.valueOf((String)session.getAttribute("vehiclequantity"));
Similar Messages
-
How to convert string to an integer in SQL Server 2008
Hi All,
How to convert string to an integer in sql server,
Input : string str="1,2,3,5"
Output would be : 1,2,3,5
Thanks in advance.
Regards,
SunilNo, you cannot convert to INT and get 1,2,3 BUT you can get
1
2
3
Is it ok?
CREATE FUNCTION [dbo].[SplitString]
@str VARCHAR(MAX)
RETURNS @ret TABLE (token VARCHAR(MAX))
AS
BEGIN
DECLARE @x XML
SET @x = '<t>' + REPLACE(@str, ',', '</t><t>') + '</t>'
INSERT INTO @ret
SELECT x.i.value('.', 'VARCHAR(MAX)') AS token
FROM @x.nodes('//t') x(i)
RETURN
END
----Usage
SELECT * FROM SplitString ('1,2,3')
Best Regards,Uri Dimant SQL Server MVP,
http://sqlblog.com/blogs/uri_dimant/
MS SQL optimization: MS SQL Development and Optimization
MS SQL Consulting:
Large scale of database and data cleansing
Remote DBA Services:
Improves MS SQL Database Performance
SQL Server Integration Services:
Business Intelligence -
How to convert String (dd-MMM-yyyy) to oracle.jbo.domain.Date
Hi
Could you please tell how do I convert String of date in format dd-MM-yyyy to ADF date? Please show me some sample.
Thankshttp://javaalmanac.com/egs/java.text/FormatDateDef.html
Once you have a java.util.Date you can convert it to oracle.jbo.domain.Date. (see http://www.fifkredit.com/bc4jdoc/rt/oracle/jbo/domain/Date.html) -
Hello,
I've a problem with Essbase(Planning?) Scripts on version 9.3. It looks simple but I do not find any (clean) solution :
On my Essbase database, I have a member called "Reference_Date" on my axis Indicators. It is a date data type, that is to say, it displays a number corresponding to a YYYYMMDD format. For example : 20091029 for October 29th 2009.
In calc scripts I often need to compare the month included in that "Reference_Date" with the current Member of my Time Axis (I have 12 Months members based on the format M02 for February for example). The final aim is to calculate a number of complete years since that "Reference_Date".
But theses two elements are not of the same "type" (one is a number value and the other is a "member" in Time Axis). So I'm forced to convert one of this two elements in order to compare it.
For example I can extract the month value of the "Reference_Date"' and put an "M" before it to have a Time member equivalent or I can convert the member Name M10 to a number (10))
in both cases I have the same type problem : I don't know how to convert a string into a number nor how to convert a number into a string.
(For example @CONCATENATE doesn't work with numbers). and that my only remaining problem.
I didn't find any Essbase Function which do this (conversion number <=>string).
Is anyone have an Idea ?
Thanks for your help
Best regardsI don't know any way for you to compare your data against your metadata. Not directly. To me it makes little enough sense to try that I'm not surprised the developers didn't provide for it.
I've converted member names to strings, manipulated the strings (calc script functions are not good at this), and turned them back into member names, but that's really the only use I've had for string manipulation. I don't think an equivalency operator even exists for string data. And I see no way to begin thinking of a member name, once converted to a string, as a number.
It makes even less sense to me to try thinking of a data value as a string. Even text values in Sys 11 are stored as numbers. Not encoded characters, but just a number to look up somewhere.
I think you can do what you want though, with something like this...
IF (@ISMBR("FY08"))
vYr = 2008;
ELSEIF (@ISMBR("FY09"))
vYr = 2009;
ENDIF;
IF (@ISMBR("M01"))
vMth = 1;
ELSEIF (@ISMBR("M02"))
vMth = = 2;
ENDIF;
"Years_Since_Reference" = ((vYr * 100) + Mth) - ("Reference_Date" / 12);
Obviously, the math will need some work, coz that doesn't actually work, but the logic above essentially turns your metadata into numbers, which is what you are after.
Good luck,
-- Joe -
How to convert string value to date
hi,
in my application user enters the date in dd/MM/yyyy formatt..before submtiing to some external server i haveto formatt it to MM/dd/yyyy format bcos it accepts olny this formatt.when i retrieve the string date in my jsp using Sring date = req.getParameter();
i want to convert this date to MM/dd/yyyyy formatt.
Regards,Sring date = req.getParameter();
string formatted = date.substring(0,3) + "/" + date.substring....
? -
How to convert varchar to int in MSSQL?
Hi,
I am using mssql. how can i convert varchar to int type.
for eg.
id (int) txt(varchar)
1 ---------10
2 ---------10a
3 ---------10b
i want to convert varchar to int. HOw? pls explain with query.
Thanks
edward"10a" wouldn't normally be considered a number. So what sort of number do you expect it to be?
Other than that you can look at the substring and convert/cast functions. -
How to convert string to 16 characters.
My external unique id is too long, more then 30 characters.
How to convert it to 30 characters using sql?You should find another extranal system id in order to mantain uniqueness, but you could also use the name field (that supports 50 characters), but it deppends of which entity you are using.
Which is the entity? and...if the only way to insert data is by web services the you could use more than one field to verify uniqueness since you can query more than one field at the time.
Hope it helps
Kim. -
How to converts string into numbers
As a beginer now i am asking the above question?
now i am working with XCode 4.3I responded to you here > How to convert pages document into...: Apple Support Communities
Please do not start duplicate topics -
How to convert String array into int.
void getSoldSms(Vector vecSoldSms)
String str[]=new String[vecSoldSms.size()];
String words[]=new String[str.length]; // String array
for(int i=0;i< vecSoldSms.size();i++)
str=(String)vecSoldSms.get(i);
} //End for
for(int i=0;i<str.length;i++)
words = str[i].split("\\|\\|");
System.out.println();
for(int j=0;j<1;j++)
int count[str.length]=Integer.parseInt(words[i]);
System.out.print(count[j]*advance_count);
} // end inner for loop
} //End for
} //End function getSoldSms
how do i convert words which is a string array into int type. i kno string can be converted into int using interger.parseint. but wat abt string arrays??? plz help me out with the above code.i did tht its still giving the same errorFor Heaven's sake, what about taking a second to try to understand the code you're copying first? If you really can't fix the error yourself, you have a more serious problem than just convertingStrings to ints.
And if you want { "1", "2", "3" } to be 123:
StringBuffer b = new StringBuffer();
for (int i = 0; i < array.length; i++) {
b.append(array);
int result = Integer.parseIn(b.toString()); -
How to convert String to date ? - MS access
I want to convert a string "10-10-08" [ MM-dd-yy ] into date type in same format to use that in query statement [MS Access].
In table , I am using field called "date" with "MM-dd-yy" format.
Example:
<%@ page import= "java.sql.*"%>
<%@ page import="java.text.SimpleDateFormat"%>
<%@ page import="java.util.Date"%>
<%
String d="10-9-08";
SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yy");
Date date = dateFormat.parse(d);
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:bspipdb");
Statement st = con.createStatement();
ResultSet rs=st.executeQuery("Select * from datacollectdb where date='"+date+"'");
%>
I am getting "java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Data type mismatch in criteria expression.
" error.
How to use the string "10-10-08" in where statement?sangee wrote:
I want to convert a string "10-10-08" [ MM-dd-yy ] into date type in same format to use that in query statement [MS Access].
In table , I am using field called "date" with "MM-dd-yy" format.I think a column named "date" is a very bad idea. Not just because "date" is a keyword. It's not very self-describing. How about "birth_date" or "hire_date" or "went_on_my_first_date"?
Example:Scriptlets? There's your second mistake.
<%@ page import= "java.sql.*"%>
<%@ page import="java.text.SimpleDateFormat"%>
<%@ page import="java.util.Date"%>
<%
String d="10-9-08";The way you're doing things, "2-40-99" is a valid date. Is that what you want?
SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yy");
Date date = dateFormat.parse(d);
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:bspipdb");Wrong again. Better to use a PreparedStatement and bind that Date.
Statement st = con.createStatement();
ResultSet rs=st.executeQuery("Select * from datacollectdb where date='"+date+"'");
%>
I am getting "java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Data type mismatch in criteria expression.
" error.
How to use the string "10-10-08" in where statement?Use PreparedStatement and bind.
Your JSP should not be getting a database connection. Pool it.
Your code is wrong on every level. You don't close resources, you don't separate you app into layers, your database code is not in a POJO. I'd recommend that you throw this away and start again.
% -
How to convert string yyyy-mm-dd to date dd/mm/yyyy
Hi Experts,
I am getting date in the form of string 2007-06-30 from R/3. How I can convert this string into a date in the form 30/06/2007. I have to use java.sql.Date.
Following code is not working. As for executing the following code, I have to use java.util.date. I can not use java.util.date as the Web Dynpro program uses java.sql.Date.
SimpleDateFormat sdf1 = new SimpleDateFormat("dd/mm/yyyy");
Date d = null;
d = sdf1.parse(wdContext.currentContextElement().getDateClicked());
Regards,
GaryHi,
If I understood you right you need this date typed as a java.sql.Date?
String ds=wdContext.currentContextElement().getDateClicked();
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
java.sql.Date sd = new java.sql.Date(sdf.parse(ds).getTime());
Four remarks:
We "transport" the Date from java.util.Date to java.sql.Date as milliseconds since January 1, 1970, 00:00:00 GMT. See contract of java.sql.Date.
You used dd/mm/yyyy instead of dd/MM/yyyy, but mm are the minutes and not the months, see the contract of [SimpleDateFormat|http://java.sun.com/j2se/1.4.2/docs/api/java/text/SimpleDateFormat.html] (JSE 1.4) .
You do not set the time zone for DateFormat, so you use TimeZone.getDefault(). This is your JVM's default time zone and depends (if you did not change it) on your systems time zone. F.e. I live in Germany, the time zone of my system is currently MEZ (which is UTC+1) since we left the daylight saving time zone last sunday. Since you only parse the date and not the time zone f.e., you expect the date (the string) to be in your default time zone. If this is not the case, you have to set the time zone to the time zone you expect the date to be in (f.e. sdf1.setTimeZone(TimeZone.getTimeZone("UTC"));) before parsing the date. This might seem marginal in your case, but may produce unexpected results with date comparisons and date parsings around the day change.
SimpleDateFormat is not thread-safe, so please do not cache it! This is not mentioned in the Javadoc of JSE 1.4, but can be looked up at [Sun's bug database|http://bugs.sun.com/bugdatabase/view_bug.do;jsessionid=96669461418011fffffffffb70dd6b6e88eb5?bug_id=4264153]. If you are not satisfied with the standard Date & Time handling (f.e. performance reasons) you might want to consider an alternative like [Joda Time|http://joda-time.sourceforge.net/index.html] which is published under [ASF 2.0 license|http://joda-time.sourceforge.net/license.html]
Best regards,
Fabian -
How to convert String "(50-10)" to number and get output as 40 as a number.
Hi,
I have a string with formula, eg: String formula="(ABC-DEF)";
I replaced the values of ABC and DEF in the above string now I have formula="(50-10)";
How can I calculate the above value and get output (40)as a number like int or double.
Regards
Alex.
Edited by: AlexRaju on Dec 8, 2009 10:11 AMValues for ABC and DEF are coming from different java collections like HashMap or ArrayList etc. The formulas can be complex which need some string manipulations and atlast I will get a string like "(50-10)" or "(20+(Math.SQRT(25)/Math.Power(2,3)))". Here all the numbers are values for variables like ABC or DEF coming from different hashmaps and arraylists based on different conditions.
Thanks
Alex.
Edited by: AlexRaju on Dec 8, 2009 10:36 AM -
How to convert string to date based on regional settings
How can I convert a simple string ("01/04/2003" or "01-04-2003" or "01 April 2003") to a date formatted according to the regional settings?
For example, if my regional settings is set to "dd mm yyyy", the result will be 01 as dd, 04 as mm and 2003 as yyyy.
If the regional settings is set to "mm dd yyyy", the result will be 01 as mm, 04 as dd and 2003 as yyyy
Hope someone can help.
Thanks in advance.I think you have the wrong idea about how Oracle stores dates. Any date in Oracle is stored in a seven byte representation
SQL> SELECT DUMP(last_hire_dt) FROM emp_t WHERE rownum = 1;
DUMP(LAST_HIRE_DT)
Typ=12 Len=7: 119,191,5,6,1,1,1This storage is independent of the NLS date settings. The TO_CHAR(dt,format) and TO_DATE(str,format) functions serve to translate this internal representation into the external presentation supplied by the format. So, if you need to translate a date that is contained in a string into an Oracle date datatype, you need to tell Oracle how to parse the string.
If the string format matches your NLS_DATE_FORMAT parameter, Oracle will do this implicitly. Otherwise, you need to tell Oracle explicitly which piece is which. For example, is the string (ignoring the problem of two digit years :-) ):
'02/01/03'
Feb. 1, 2003, Jan. 2, 2003, or Mar. 1, 2002? If your date format is dd-MON-yyyy as mine is, then:
SQL> SELECT TO_DATE('02/01/03') FROM dual;
SELECT TO_DATE('02/01/03') FROM dual
ERROR at line 1:
ORA-01843: not a valid month
SQL> SELECT TO_DATE('30-Jan-2003') FROM dual;
TO_DATE('30
30-JAN-2003HTH
John -
How to convert string date to long (or Unix date)?
I'm having difficulty in converting a user supplied date in the format mm/dd/yyyy to a long format such as 1035462807000. Since it's being entered by the user I can't just use the current system date. Any suggestions? Thanks!
DaveHi man, tsith Gave a good beggining, why dont you try the next code
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.util.Date;
public class DateToUnix{
public static void main(String args[]){
getDateAsLong();
public static void getDateAsLong(){
String sStartDate = "12/01/2002";
Date theDate;
DateFormat sdf= new SimpleDateFormat("MM/DD/yyyy" );
try {
theDate = sdf.parse( sStartDate );
}catch(ParseException e ){
theDate = new Date();
System.out.println( "Converted Start Date:" + theDate.getTime());
}No mather how you get a Date object, the value you are looking for is a long, and you can get it from such object with the getTime() method.
Take a look at the Java API @ [http://java.sun.com/j2se/1.4.1/docs/api/java/util/Date.html#getTime()] -
UDF: How to convert string with sign into integer
Hi
I have a number e.g 123456 (xsd:string) and sign - or + (xsd:string) and both are concatenated and fed into UDF. I want this string to be converted into Integer. If I use the below kind of statement it is not working and throwing the below error.
int BilledAmount = Integer.parseInt(b[0]);
Exception:
RuntimeException in Message-Mapping transformation: Exception:[java.lang.NumberFormatException: For input string: "+000000000006684"]
Any guess on this issue ??
Regards
KumarHey buddy,
Hope you are doing gr8, I'm getting ready leaving this friday..:-)
Aamir b[0] doesn't correspond to the first character of the string. He has multiple input values, hence he is using Context. See my structure and UDF I mentioned above, actually the above is his requirement I believe.
Yes, If you have + symbol in front of String and if you try to conver to Integer object it will throw an exception. Coz it doesn't make sense to add + symbol at all. if you don't have any sign in front it implies it's positive, am I right? But in case of negative, you need to explicitly include - symbol in front.
This is the reason that java couldn't able to convert the string that has + symbol in front, but it can in case it has - symbol in front.
I hope it clears a bit..
raj.
Maybe you are looking for
-
Can anyone tell me if they know where I can find a hard case for the new Retina Macbook Pro?
Can anyone tell me where I can find a hard case for a new 15" retina macbook pro?
-
How can I convert a Microsoft Word 2004 file to Microsoft Word 2011? Most of the time the prompt says that the old Word is not supported anymore.
-
Adobe thumbnail preview does not work in adobe acrobat 8.0
Hi , I have user login using his profile and suddenly his not able to view thumbnail preview of PDF file using navigation panel but the thumbnail preview work in administrator profile. Si, I guess some settings blocking in user profile to view thumbn
-
Cannot change partition size on firewire external drive
My external firewire drive has three partitions, one of which is used for Time Machine and is out of space; the third one is empty. When I use Disk Utility to try to increase the size of the second partition without erasing its contents, I get the me
-
How to export a reference to a table line from a method?
Hello, I have a class which has a hashed table as an attribute. I want to retrieve a line from the table using the key and return a reference to the table line. I want changes to the return value of the method to be directly reflected in the table, a