Conversion.

Q

qwert

Hello,

how do I convert types? Something like the following, but it is not possible
in Java:

Object objObject = (String) "No integer";
int intTemp;

try
{
intTemp = (int) objObject; // Suppose to cause exception....(this line
doesn't compile)
}
catch
{
intTemp = 0;
}

Basicly, I have an object which can contain any type and a set of functions
that must convert it to a specific type. If the conversion is not possible,
the functions must return a default value,

thanks for any info.
 
P

Patricia Shanahan

qwert said:
Hello,

how do I convert types? Something like the following, but it is not possible
in Java:

Object objObject = (String) "No integer";
int intTemp;

try
{
intTemp = (int) objObject; // Suppose to cause exception....(this line
doesn't compile)
}
catch
{
intTemp = 0;
}

Basicly, I have an object which can contain any type and a set of functions
that must convert it to a specific type. If the conversion is not possible,
the functions must return a default value,

thanks for any info.

Well, what you wrote is very close to one of the ways of doing it in
Java, going ahead with the conversion and catching the
ClassCastException if it fails.

However, I would do it with a precheck:

if(objObject instanceof Integer){
return ((Integer)objObject).intValue();
}else{
return 0;
}

Patricia
 
D

Dimitri Maziuk

qwert sez:
Hello,

how do I convert types? Something like the following, but it is not possible
in Java:

Object objObject = (String) "No integer";
int intTemp;

try
{
intTemp = (int) objObject; // Suppose to cause exception....(this line
doesn't compile)
}
catch
{
intTemp = 0;
}

You're implying that it's possible in some language other than Java.
I'm curious: what language is that?

Dima
 
Q

qwert

Well, what you wrote is very close to one of the ways of doing it in
Java, going ahead with the conversion and catching the
ClassCastException if it fails.

Very close, but not close enough. It doesn't compile. Compiler says
"inconvertible types" when I do the following:

Object objObject;
int intTemp = (int) objObject;
However, I would do it with a precheck:

if(objObject instanceof Integer){
return ((Integer)objObject).intValue();
}else{
return 0;
}

This code doesn't work either. Is it pseudo-code?

Thanks.
 
P

Patricia Shanahan

qwert said:
Very close, but not close enough. It doesn't compile. Compiler says
"inconvertible types" when I do the following:

Object objObject;
int intTemp = (int) objObject;

You need to think through what classes of objects you want to convert to
integer, and what that means in each case. As in the code I posted, you
can get the int equivalent of an Integer by calling its intValue() method.
This code doesn't work either. Is it pseudo-code?

Thanks.

It is working code doing my best guess at what you are trying to do. Of
course, you will need to modify it to fit how you want to use it. As
written, I'm assuming it fits in a conversion method that returns int.
Here is my test program:

public class TestConvert {
public static void main(String[] args) {
System.out.println(convertToInt(new Integer(33)));
System.out.println(convertToInt("xxx"));
}
static int convertToInt(Object objObject){
if(objObject instanceof Integer){
return ((Integer)objObject).intValue();
}else{
return 0;
}
}
}

Patricia
 
S

Stefan Ram

Dimitri Maziuk said:
You're implying that it's possible in some language other than
Java. I'm curious: what language is that?

I guess VBA does not count as a language, nevertheless:

Sub Test()
Dim objObject As Variant
objObject = "No integer"
Dim intTemp As Integer
On Error GoTo Handler
intTemp = objObject
GoTo Over
Handler:
Debug.Print "caught"
intTemp = 0
GoTo Over
Over:
Debug.Print intTemp
End Sub

prints

caught
0

Using »objObject = "22"« in the third line will make it print

22
 
Q

qwert

Heya,

Aah. Your thing is starting to work (new to Java...):

if( objInteger intstanceof String )
{
return Integer.parseInt((String)objInteger);
}
else
{
return ((Integer) objInteger.intValue();
}
....
catch
return 0;

Not sure what happens with other types like long's. But have a starting
point now. Thanks everyone for help and patience.




Patricia Shanahan said:
qwert said:
Very close, but not close enough. It doesn't compile. Compiler says
"inconvertible types" when I do the following:

Object objObject;
int intTemp = (int) objObject;

You need to think through what classes of objects you want to convert to
integer, and what that means in each case. As in the code I posted, you
can get the int equivalent of an Integer by calling its intValue() method.
This code doesn't work either. Is it pseudo-code?

Thanks.

It is working code doing my best guess at what you are trying to do. Of
course, you will need to modify it to fit how you want to use it. As
written, I'm assuming it fits in a conversion method that returns int.
Here is my test program:

public class TestConvert {
public static void main(String[] args) {
System.out.println(convertToInt(new Integer(33)));
System.out.println(convertToInt("xxx"));
}
static int convertToInt(Object objObject){
if(objObject instanceof Integer){
return ((Integer)objObject).intValue();
}else{
return 0;
}
}
}

Patricia
 
D

Dimitri Maziuk

Stefan Ram sez:
I guess VBA does not count as a language, nevertheless:

Yeah, but then you might know that VB isn't exactly a statically
typed language... OP clearly does not.

Dima
 
C

Chris Uppal

qwert said:
Basicly, I have an object which can contain any type and a set of
functions that must convert it to a specific type. If the conversion is
not possible, the functions must return a default value,

I think it's unlikely that you have a valid need for this unless you are
writing an implementation of a different language (one with a different type
system). I suggest you review your design and see /why/ you need it, and
whether there is a better way of doing things.

If you want to, or have to, stick with this approach then the only way you can
do it, is by defining a special kind of object which contains the data in one
(or more) common forms, and converts as necessary. E.g.

class Variant
{
private String m_value;

public void set(String s) { m_value = s; }
public String get() { return m_value; }
public void set(int i) { m_value = Integer.toString(i); }
public int getInt() { return Integer.parseInt(m_value); }
public void set(double d) { m_value = Double.toString(i); }
public int getDouble() { return Double.parseDouble(m_value); }
// etc...
}


-- chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top