Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Using a string when creating an object.

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Using a string when creating an object.

    Say I have a list of plants and a bunch of plant<Plantname> classes.
    I want to be able to use a plant from the array to create an object for referencing a plant<Plantname> class.

    My first class.
    public class plantList{
    	public static void main(String[] args){
    		String[] plantList ={"Tree","Bush","NotherPlant","NotherNotherPlant"};
    	}
    }

    My other classes go along these lines:
    public class plantTree{
    	String Color=Brown,OtherStuff=DescriptionOfOtherStuff;
    }

    I wish it was as simple as putting:
    plant(plantList[/*number*/]) goplant = new plant(plantList[/*number*/])

    This is obviously not the actual stuff I would put in here, but just an example.

    How would I go about doing this?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Using a string when creating an object.

    I want to be able to use a plant from the array to create an object
    Plant[] somePlants = new Plant[100]; // Create an array of Plant
    ...
    somePlants[i] = new Plant(...); // put some Plant objects into the array
    ...

    NewObj newObj = new NewObj(somePlants[y]); // create a new object using an element from the array

  3. #3
    Junior Member
    Join Date
    Jul 2011
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Using a string when creating an object.

    But the string from the array needs to be in the object after plant.
    So if I have plantList[0] as tree and I'm making the new object to get info from plantTree.java, I need the computer to read it as:

    plantTree goplant = new plantTree;

    where the Tree in plantTree is pulled from the array.
    This is so if I want to access plantTree, or any other plant<plantname>, from main (for getting variables to print and such), I would just have the new object use the array.

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Using a string when creating an object.

    Are you asking how to create a specific object based upon a String value? Personally I dislike this algorithm but you can achieve it using a if/else if statement.
    if string is tree {
        create plantTree object
    } else if string is bush {
        create plantBush object
    } etc
    Improving the world one idiot at a time!

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Using a string when creating an object.

    Your description of your problem is confusing.
    string from the array needs to be in the object after plant.
    What does "the object after plant" mean? Can you give an example

    plantList[0] as tree and I'm making the new object to get info from plantTree.java
    Does the plantList array contain tree objects? Is tree a class?
    How does the plantTree.java source file relate to this?

    where the Tree in plantTree is pulled from the array.
    Your changing case is confusing. Is Tree a class and is it the same as tree?

  6. #6
    Junior Member
    Join Date
    Jul 2011
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Using a string when creating an object.

    1st question:

    The object after plant means that I want to have the word plant (the prefix for all my plant<plantname> classes) to have the value of the string directly after it.
    ie, I have:
    plantTree goplant = new plantTree
    but I want to be able to exchange tree with any other string.
    sort of like: (this doesn't work but it gets my point across)
    plant<followed directly by>plantList[<number here>] goplant = new plant<followed by>plantList[<number>]


    2nd question:

    The plantList array contains Tree as a string. (in my example at plantList[0])
    plantTree is a separate class that needs to be accessed by main. (for retrieving variables and such)
    plantTree,java is the .java file for the class plantTree.

    3rd question:

    It sort of is, but it's actually plantTree.
    The lowercase Tree is a typo, it should all be the same Tree with a capital "T".


    Sorry about this, I'm terrible at asking for help and explaining things to people.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Using a string when creating an object.

    object after plant means that I want to have the word plant (the prefix for all my plant<plantname> classes) to have the value of the string directly after it.
    ie, I have:
    plantTree goplant = new plantTree
    but I want to be able to exchange tree with any other string.
    That sounds like a macro or preprocessor activity. Java does not have that.
    You will have to explicity code all your class definitions giving them each a unique name as you wish.
    Class names in java should all start with capital letters by convention.
    The class names should be like PlantTree.

    The plantList array contains Tree as a string.
    Let me reword that. The String array named plantList contains the String "Tree".
    For example:
    String[] plantList = new String[] {"Tree"}; // define an array containing the String "Tree"

    plantTree is a separate class that needs to be accessed by main
    Naming conventions require that the class name be: PlantTree
    I assume by main you mean the main() method. Where will you put the definition of the PlantTree object variable? There can be problems defining a variable in a method. It will only be known in that method.

  8. The Following User Says Thank You to Norm For This Useful Post:

    ZeroLRS (July 13th, 2011)

  9. #8
    Junior Member
    Join Date
    Jul 2011
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Using a string when creating an object.

    Darn, I plan to have several hundred "plant<plantname>" classes.
    I felt like being lazy and not making a VERY long else if...

    All that is contained in the "plant<plantname>" classes is several variables.
    I just wanted to relay that info from the classes to main.

  10. #9
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Using a string when creating an object.

    Then you need to think of a better design. I said I disliked the suggested solution because it stinks. Creating objects based upon some String value is often asked about by n00bs and usually the wrong way to solve the problem.
    Improving the world one idiot at a time!

  11. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Using a string when creating an object.

    All that is contained in the "plant<plantname>" classes is several variables.
    What are the values you want to save?
    Perhaps one class can contain the data for each plant.
    Then use a Map to hold the objects for each type of Plant.
    The key would be the plant name and the value would be the object with the values.

  12. The Following User Says Thank You to Norm For This Useful Post:

    ZeroLRS (July 13th, 2011)

  13. #11
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Using a string when creating an object.

    Then you need to think of a better design. I said I disliked the suggested solution because it stinks. Creating objects based upon some String value is often asked about by n00bs and usually the wrong way to solve the problem.
    Improving the world one idiot at a time!

Similar Threads

  1. Creating an object...
    By RodePope4546 in forum Object Oriented Programming
    Replies: 6
    Last Post: June 27th, 2011, 06:44 AM
  2. Creating Servlet object
    By tcstcs in forum Java Servlet
    Replies: 3
    Last Post: May 9th, 2011, 02:13 AM
  3. store a list/string for each object
    By tommy in forum Object Oriented Programming
    Replies: 5
    Last Post: May 28th, 2010, 09:24 AM
  4. Convert object to String
    By innspiron in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 15th, 2010, 08:48 AM
  5. Create a CLOB object with the string value
    By oshoarun in forum JDBC & Databases
    Replies: 0
    Last Post: March 6th, 2010, 02:54 AM