aspose file tools*
The moose likes Beginning Java and the fly likes How can I create an object based on input Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How can I create an object based on input" Watch "How can I create an object based on input" New topic
Author

How can I create an object based on input

John Lockheart
Ranch Hand

Joined: Oct 13, 2006
Posts: 115
I've always created objects by specifying their class and parameters. However now, i'm reading from a text file. Each line will have what type of object then a comma, then it's parameters. I've created two Strings to hold this information object and parameters. Using indexOf to find the comma and substring to assign values to them. I want to add an "object" with "parameters" to a vector. Although I won't know what object it is, or it's parameters until I read of the text file. All my classes will be in a seperate java file. how should I be doing this?

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11230
    
  16

You might be able to do it with the Factory Pattern. a factory is basically an object whos job is to create other objects. by passing the parameters to it, it figures out what you really want, makes it, and returns a reference.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
John Lockheart
Ranch Hand

Joined: Oct 13, 2006
Posts: 115
interesting...do you know a good link for further information or should I just cruise the net?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11230
    
  16

I am not a pattern expert by anymeans. but there are many good books on them. there is the head first design patterns book, which is great for beginners. The GoF (that's Gang of Four) Design patterns book is the most famous.

check out this FAQ for some online resources.
John Lockheart
Ranch Hand

Joined: Oct 13, 2006
Posts: 115
I've been making attempts but I don't think i'm even close to a solution. Is this the fashion in which i'm supposed to proceed?

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
Not at all. It is much simpler than that. You need to do something like this:-Two problems here:-
  • When you get the Object back you have to cast it to the correct class.
  • I can't remember how you set up any constructor parameters.
  • Anybody else remember how to do it?

    CR
    John Lockheart
    Ranch Hand

    Joined: Oct 13, 2006
    Posts: 115
    well this is what i have, and i think it's working. but is what's stored in my vector an object that I can perform operations on or a reference? do I have to cast it? When I print out the contents of the vector, everything seems to have worked. Also i put all the necessary classes in the same java file, i didn't write it below because it takes up alot of space.

    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38363
        
      23
    Thank you.
    Difficult to understand what you are doing, when your Threat class doesn't appear to be in the file, so I can't run the class.
    Your many if-else blocks don't constitute good object-oriented programming, I am afraid.
    I can't actually remember how you set up objects which have constructor parameters.
    You would have to do something like this. . but as I said I can't remember how to set up the parameters.



    Try this sort of thing:-I looked up the Class.newInstance method, and couldn't find any sign of it using consructor parameters.
    And you have to get the class names spelt exactly right, and all the parameters entered correctly, or your whole app will collapse.

    Try that and see whether you can get it to work.

    CR

    [ Edit for minor errors ]
    [ March 02, 2007: Message edited by: Campbell Ritchie ]
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38363
        
      23
    I am not happy about your readFile method which creats another A4Q2 object. Apart from the fact that A4Q2 is a bad name for a class, you are going to get an infinite progression, and the class will crash with an OutOfMemoryError when you instantiate more than about 30000000 objects.
    John Lockheart
    Ranch Hand

    Joined: Oct 13, 2006
    Posts: 115
    I think i have everything working. I've also created a method which reads instructions from a file. The only ones it accepts at the moment are "add", "print", "remove", "increase". I can't get increase or remove to work because I have to search the vector. I thought i could use a for loop with the proper vector commands. But are there other vector commands which can search for an "instanceof Malware" (malware being an abstract class for 2 types of objects, which are the only type you can increase). Then for remove, I also need to search the vector and match the name of an object, with the name given by the input text file (using the getName method inside the classes). Without using a for loop, what's the best way?

     
     
    subject: How can I create an object based on input