LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 02-14-2006, 12:25 PM   #1
MRMadhav
Member
 
Registered: Nov 2005
Location: Mauritius
Distribution: PCQLinux, SUSE Linux,Fedora Core 5, Fedora Core 6, Knoppix Live, Kubuntu Edgy, PCLinuxOS
Posts: 167

Rep: Reputation: 30
Question Getting a class From a String in Java


Hi,
I am writing an application that reads a text file and reads each line in a different string, then that program should read that string and load the class that has exactly the name that is contained in the string(the class always exists). Now my problem is that i need to find a way to load the class, whose name equals to the string contents, automatically and without and other condition like the switch case conditions or else if conditions which are lenghty and which can create problems when the text file becomes really long.

Any one can help?????
 
Old 02-14-2006, 02:04 PM   #2
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Ubuntu, OEL
Posts: 9,165

Rep: Reputation: 243Reputation: 243Reputation: 243
Quote:
Originally Posted by MRMadhav
Hi,
I am writing an application that reads a text file and reads each line in a different string, then that program should read that string and load the class that has exactly the name that is contained in the string(the class always exists).
That's easy.
Quote:
Now my problem is that i need to find a way to load the class, whose name equals to the string contents, automatically and without and other condition like the switch case conditions or else if conditions which are lenghty and which can create problems when the text file becomes really long.

Any one can help?????
is this your homework ?
Can you elaborate about why writing structured programs create problems ?
 
Old 02-14-2006, 11:04 PM   #3
MRMadhav
Member
 
Registered: Nov 2005
Location: Mauritius
Distribution: PCQLinux, SUSE Linux,Fedora Core 5, Fedora Core 6, Knoppix Live, Kubuntu Edgy, PCLinuxOS
Posts: 167

Original Poster
Rep: Reputation: 30
Thanks for replying! Well i am trying to generate a menu out of the text file and i wanted to know how to load the classes out of the text(String) from the text file.
If its easy then you will surely be able to help!
 
Old 02-16-2006, 05:05 AM   #4
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Ubuntu, OEL
Posts: 9,165

Rep: Reputation: 243Reputation: 243Reputation: 243
See:
http://java.sun.com/j2se/1.5.0/docs/...a.lang.String)

Last edited by jlliagre; 02-18-2006 at 03:30 AM.
 
Old 02-17-2006, 08:56 PM   #5
mrcheeks
Senior Member
 
Registered: Mar 2004
Location: far enough
Distribution: OS X 10.6.7
Posts: 1,690

Rep: Reputation: 50
Code:
Object obj = Class.forName(stringClassName).newInstance();
JMenu menu = (JMenu)obj;
 
Old 02-18-2006, 05:50 AM   #6
MRMadhav
Member
 
Registered: Nov 2005
Location: Mauritius
Distribution: PCQLinux, SUSE Linux,Fedora Core 5, Fedora Core 6, Knoppix Live, Kubuntu Edgy, PCLinuxOS
Posts: 167

Original Poster
Rep: Reputation: 30
Lightbulb

Thank you all for your answers and Special Thanks to mrcheeks as I used what he gave and that was the solution! Thanks!

Last edited by MRMadhav; 04-03-2006 at 06:45 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Questions about custom C++ string class. lowpro2k3 Programming 1 08-23-2005 12:06 PM
is there a c++ binary safe string class? Thinking Programming 3 08-11-2005 06:18 PM
Linking problem with string class live_dont_exist Programming 4 04-09-2005 08:07 AM
std string class and printf The_Nerd Programming 3 06-28-2004 10:46 PM
java test if string in string array is null. exodist Programming 3 02-21-2004 01:39 PM


All times are GMT -5. The time now is 04:39 AM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration