LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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
  Search this Thread
Old 07-19-2014, 10:13 AM   #1
Gflames
LQ Newbie
 
Registered: Nov 2012
Location: Malawi
Posts: 25

Rep: Reputation: Disabled
creating an object reference name from String variable in Java


I want to accept input from the console as a String and use it to name objects, ie
String objectName = input.next();
//use the content of variable objectName as a reference name like this
Class +objectName = new Class;
 
Old 07-19-2014, 11:09 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,869
Blog Entries: 1

Rep: Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870
I think it is 'associative memory' (or 'hash') indexed by strings, what might be comparable to what you want.

Last edited by NevemTeve; 07-19-2014 at 11:11 AM.
 
Old 07-19-2014, 12:26 PM   #3
Gflames
LQ Newbie
 
Registered: Nov 2012
Location: Malawi
Posts: 25

Original Poster
Rep: Reputation: Disabled
reference please or use it in an example
 
Old 07-20-2014, 03:42 PM   #4
Gflames
LQ Newbie
 
Registered: Nov 2012
Location: Malawi
Posts: 25

Original Poster
Rep: Reputation: Disabled
after some reserch I have managed to come up with this solution but it is giving me the followin error
ityRoutes.java:19: error: incompatible types: CAP#1 cannot be converted to Class
Class city1 = Class.forName("City").newInstance();
^
This is part of the code
System.out.println("Enter Number of Nodes or Cities");
Scanner input = new Scanner(System.in);
int numberOfCities = input.nextInt();
for(int i = 0; i < numberOfCities; i++)
{
System.out.println("Enter Name For City: "+i);
String cityName = input.next();
try{
Class city1 = Class.forName("City").newInstance();
city1.getCityName(cityName);
System.out.println("Enter cities Connected to" +cityName +"\nEnter 0 if none or Done");
} catch (ClassNotFoundException e) {

} catch (InstantiationException e) {

} catch (IllegalAccessException e) {
}

}
 
Old 07-20-2014, 11:40 PM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,869
Blog Entries: 1

Rep: Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870
Code:
import java.util.HashMap;

class City {
    String state;
    City (String state) {
	this.state= state;
    }
    String getState () {
	return state;
    }
};

public class HMtest {
    static void TestGet (HashMap<String, City> map, String key) {
	City c= map.get (key);
	if (c!=null) System.out.println (key + " found in " + c.getState ());
	else         System.out.println (key + " not found");
    }

    public static void main (String args[]) {
	HashMap<String, City> named_cities = new HashMap<String, City>();
	named_cities.put ("Berlin", new City("Germany"));
	named_cities.put ("Roma", new City("Italy"));
	TestGet (named_cities, "Madrid");
	TestGet (named_cities, "Roma");
    }
}
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Java: Copying String Object CoderMan Programming 1 01-14-2010 01:09 AM
java: intialize object/variable inside constructor vs outside kpachopoulos Programming 5 09-05-2006 12:05 AM
Object Reference problem in Java servlets Hockeyfan Programming 1 06-16-2005 01:36 AM
Pass object variable to anothor class in JAVA hus Programming 2 05-11-2005 04:04 AM
java object with a String and an Image exodist Programming 1 05-15-2004 12:36 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:56 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration