Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String to Int 1

Status
Not open for further replies.

scohan

Programmer
Dec 29, 2000
283
US
I know I should know this if I'm trying to write java code, but I'm an idiot. I'm setting a session variable and trying to retrieve it as an integer, but that doesn't work. Question 1: Can I set the session var using an integer (this doesn't seem to cause a problem). For example:

int rows_count = 10;
session.putValue("rows",row_count);

Question 2: How can I retrieve it as an int? For example:

if (session.getValue("rows") != null) {
row_count = (int) session.getValue("rows");
}
for (int i=1; i < row_count; i++) {


I get a 'page cannot be displayed' when trying to assign the session variable to the row_count int. Any ides? Thanks.
 
Hi,

I am unsure about this but what you can do is to put it as an Integer instead of int. You must know that int is not a class in java wheras Integer is. The same goes for double, long and others. (The only exception is String)

So if you are doubtful, just change the codes to:-

int rows_count = 10;
session.putValue(&quot;rows&quot;,new Integer(row_count));

if (session.getValue(&quot;rows&quot;) != null) {
row_count = ((Integer) session.getValue(&quot;rows&quot;)).intValue();
}

If you still get back the same error, it means that there are other problems with the page too. If there isn't, that means you can't use int :)

Regards,
Leon If you need additional help, you can email to me at zaoliang@hotmail.com I don't guaranty that I will be able to solve your problems but I will try my best :)
 
You can also try to
Code:
Integer.parseInt()
just about any String - you just have to catch the NumberFormatException (in case the String is not really a number...)

For that you'll have to change the line to something like

Code:
row_count = Integer.parseInt(session.getValue(&quot;rows&quot;).toString());

This is not really necessary here, as you have full control over what you put into the HashMap. But maybe somewhere else it helps somebody else? allow thyself to be the spark that lights the fire
 
Thanks, your suggestions did work.

Here's a followup question. I use the onclick event for an input tag in my form. I pass it an integer. I need to set a jsp session variable with the value of this variable. How can I get the javascript integer variable into the jsp code to set the session variable? One doesn't seem to recognoze the other. I have:

<script>
function copy(row)
{
selected_row=row;
<%
session.putValue(&quot;sel_row&quot;, new Integer(selected_row));
if (session.getValue(&quot;sel_row&quot;) != null) {
jsp_sel_row = ((Integer) session.getValue(&quot;sel_row&quot;)).intValue();
}
</script>
.
.
.
<INPUT name=cmd_copy type=image src=&quot;copy.gif&quot; alt=&quot;Copy Row&quot; onclick=&quot;copy(<%=i%>)&quot;>

This doesn't work - jsp_sel_row is still 0. Clues? Thanks again.
 
Hi,

Sorry for the late reply, because I was busy recently. Seriously speaking, I am actually not very good with jsp. But I consulted my friend on this and got some answers from him. I have seen him encountering this problem so this is my solution.

Firstly, the reason why it wouldn't work is because javascript and java can't communicate? Wierd huh? Well that is what my friend told me. Javascript is unable to &quot;read&quot; Java variables because they are 2 different things.

So what my friend did to solve this problem was he set the jsp variable (which is i in this case) as a hidden html input tag. Which means <input type=&quot;hidden&quot; name=&quot;i&quot; value=&quot;?&quot;>. So the javascript would be able to read this variable i since html can communicate with javascript.

I don't know if there are other ways to solve this but I think this is what you can try out.

Hope this helps,
Leon If you need additional help, you can email to me at zaoliang@hotmail.com I don't guaranty that I will be able to solve your problems but I will try my best :)
 
Thanks for the reply. I was actually just working on that very same solution. Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top