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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

cast problem 2

Status
Not open for further replies.

tyris

Programmer
Nov 2, 2000
311
FR
hi,

i do have an array called MyArray. to access to one element of the array i need of course to put an indice, that must be an int.
the problem is that i get this indice from a getAttribute method.

if i put somenthing like this :

Integer n = (Integer)request.getAttribute("myVar");
String toto = MyArray[n];

i do have this error :

Incompatible type for []. Can't convert java.lang.Integer to int.

what can i do to transform my request.getAttribute("myVar") into an int ? Best regards X-),
Elise
 
Try this Elise:
Code:
import java.util.*;

// Init an array
String[] MyArray = new String[3];
// Add some data to the array
MyArray[0] = "Test";
MyArray[1] = "Blah";
MyArray[2] = "hah";

// Cast n as (int)myVar
int  n = Integer.parseInt(request.getParameter("myVar"));
// Put the data from MyArray[n] into 'toto'
String toto = MyArray[n];
// Display it
out.println(toto);

Of course, if you want this as a JSP you'll need to use <%@ page import=&quot;java.util.*&quot;%> rather than specifically putting it into the scriptlet tags.

Also, you'll need to put a try/catch statement around your code to check for OutOfBoundsException, which you'd get if myVar was passed in at an int greater than 2 in the above example.

Hope this helps you,

Tim --
Tim <tim@planetedge.co.uk>
 
..also check for NumberFormatException in case someone does myVar=xyz!

Tim --
Tim <tim@planetedge.co.uk>
 
okay thanks
is parseInt() equivalent to intValue() ? Best regards X-),
Elise
 
No parseInt() and intValue() are not the same.
- intValue() returns the int value represented by that Object (in your case a Integer).
- parseInt(String s) returns the int represented by the string. This string must be only decimals. only the first character can be a '-'.

Hope this helps,
Steven.
 
an other problem like the other one :

myRespObj.budget is a java.lang.Float

rs.getFloat(&quot;ChgeRate&quot;) is a float.

what can i do to make an &quot;=&quot; between them ?

i tryed
myRespObj.budget = (Float)rs.getFloat(&quot;ChgeRate&quot;)
but the result is : &quot;cannot convert from float to java.lang.Float&quot;


Best regards X-),
Elise
 
try this then:
myRespObj.budget = new Float(rs.getFloat(&quot;ChgeRate&quot;));

Hope this helps,
Steven.
 
i've found something else :


Float.parseFloat(rs.getFloat(&quot;ChgeRate&quot;));


Best regards X-),
Elise
 

hi

your code looks weird... :

Float.parseFloat(rs.getFloat(&quot;ChgeRate&quot;));

because 'getFloat' is a method that return a float type

and 'parseFloat' converts its Sting argument into a float...

(that's not your case : you have got a float !)

so you're trying to convert a float to a float ... ;-)

another thing : Float.parseFloat returns a 'float' and not a 'Float' !! these are two different types ( i'm sure you already know that but i noticed you use a Float property 'budget' in your 'myRespObj' object)

you should better use basic types (like float) in your classes than wrapper classes (like java.lang.Float) that are convenient in some cases for converting from String or for putting basic variables types in a container (like a Hashtable) but are not for handling data in your objects...

hope it helps
:)

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top