Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-17-2008, 08:33 AM
Member
 
Join Date: Jun 2008
Posts: 4
sardare is on a distinguished road
runtime type conversion
Hi,

I have a mapping from variables to types. I am reading in those variables from a file as string and I need to convert them to their type dynamically. Since there are 100 or so varialbles, I don't want to code type conversion of each of them.

e.g. variable to type mapping:
public class Var {
public static Var v1 = new Var("double");
public static Var v2 = new Var("int");
public static Var v2 = new Var("String");

public Var(String type) {
_type = type;
}

public String getType() {
return _type;
}
}

Now I read in the variables from a file as strings. How do I dynamically convert them to their corresponding types?

thanks for your help/

cheers,

Samit
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-17-2008, 09:30 AM
Fubarable's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 1,289
Fubarable is on a distinguished road
cross-posted here: Java Programming - java runtime type conversion
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 08-18-2008, 08:51 AM
Niveditha's Avatar
Senior Member
 
Join Date: May 2008
Posts: 299
Niveditha is on a distinguished road
Send a message via Skype™ to Niveditha
Hi,
why dont you use a switch stmt?

Its just a suggestion i am not sure how good it will work.
__________________
To finish sooner, take your own time....
Nivedithaaaa
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 08-18-2008, 10:51 AM
fishtoprecords's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 533
fishtoprecords is on a distinguished road
Quote:
Originally Posted by Niveditha View Post
why dont you use a switch stmt?
Switch statements are poor OO, you are supposed to use polymorphism.

Of course, I backslide and use switch statements when needed.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Cast string type to int type GilaMonster New To Java 9 09-17-2008 12:43 PM
Conversion from wav to vox bozovilla Advanced Java 1 07-31-2008 07:54 AM
Doc to Pdf conversion praveen.kb Advanced Java 1 06-30-2008 12:28 PM
binary conversion thamizhisai New To Java 3 04-29-2008 01:55 PM
How to cast an Object into a specific type (Integer/String) at runtime mailtogagan@gmail.com Advanced Java 2 12-03-2007 03:04 PM


All times are GMT +3. The time now is 09:33 AM.


VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org