• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

exceptions and type casting confusion

 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, i have this problem, when dealing with this Array of Objects.I need to be able to tell whether this object is a type String or is a type Boolean, etc,
and throwing an exception when the content of the first column it's not of the type that it says on the second column, the tricky part is that every time it runs, different objects have different datatypes..
Example:

 
Jorge Bendahan
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I forgot, this is what i've done, but always catches an error, even when it supposed not to.
 
Jorge Bendahan
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've got it, here it is :
 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Some remarks:

if(arr[i][1].toString()=="boolean")



It is usually not a good idea to compare Strings with the "==" operator. What you normally want is comparing using the equals method.
if(arr[i][1].equals("boolean"))

String b = new String(arr[i][0].toString());


When you know you have a String object, it is not needed to create a new one. Just cast the object to String
String b = (String)arr[i][0];

long l = new Long(arr[i][1].toString().trim()).longValue();


There is no need to create a Long object (or any Number object) here as Long has a useful static method to parse a String
long l = Long.parseLong(arr[i][1].toString().trim());

Btw, Is there a reason you have an array of Objects? All your elements are Strings, so you could just as well have an array of Strings.
[ May 20, 2008: Message edited by: bart zagers ]
 
Jorge Bendahan
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the objects in the array aren't strings, i'm using the toString method just because the constructors of that clases(Integer, Boolean, etc) require a String parameters. But thanks for those observations.
 
bart zagers
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If they aren't Strings, what are they?
 
Jorge Bendahan
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
they are Objects, after the datatype comparison they are casted to the correspondant datatype.
 
bart zagers
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, you have an array of Objects, but the actual elements of the array will not be instances of Object, but something else.
For example if you have your array
Object[][] arr
and you do
arr[0][0] = new Long(5);
arr[0][0] will contain an object of type Long.
So again, what will be the type(s) of the elements in the array?
 
Jorge Bendahan
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Suppose i'm calling this method once, and this time the array will be:

arr[0][0]= pedro , arr[0][1] = String
arr[1][0]= 123 , arr[1][1] = int
arr[2][0]= c , arr[2][1] = char
arr[3][0]= bill , arr[3][1] = String

Now i'm calling the method for the second time and the parameter passed is:

arr[0][0]= -235 , arr[0][1] = short
arr[1][0]= 1.2 , arr[1][1] = float
arr[2][0]= asd123 , arr[2][1] = String
arr[3][0]= x , arr[3][1] = char
 
Jorge Bendahan
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As you can see the elements of the array may vary in each call, you may wonder what is this for, well i'm doing a research in uml models and ocl parsing, so different models or constraints have different elements with different datatypes and values.
 
Jorge Bendahan
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
BTW this is for academic non commercial purposes(i'm student, as you may noticed for the poor knowledge of the java api ), so i'm not "taking advantage of your knowledge", actually i'm doing this research pretty much solo(just me).
 
bart zagers
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guessed that, that's why I asked some addtional questions.
It is just that the code you showed seems not very efficient and not very elegant, although it might work.
Depending on the content of the array, there are several improvements and simplifications possible.
For example take your last example and the line
arr[1][0]= 123 , arr[1][1] = int
what is now the type of arr[1][0] (you can check it for example by printing arr[1][0].getClass().getName() )?
It is most likely either a String, which case your conversion code could be simplified to

or the object already is an Integer object, in which case you don't even need the second column and the code becomes something like
 
Jorge Bendahan
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This cant work, here is an example: arr[i][0]= 12, arr[i][1]="short"
if you use the instanceof method with Integer i will return true.

But thanks for the responses anyway, this issue has been solved, now i have some new ones
 
When all four tires fall off your canoe, how many tiny ads does it take to build a doghouse?
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic