-2

I have a question wherein a text file I have below values

Car,Car1,100,10,5,500
bike,bike2,10,1,1,100

Each vehicle is having its own class which extends parent class Vehicle.Is there a way through which I can compare the first value from the file and check for a class with the same name and then pass the values to its constructor.

currently, I am reading a text file, splitting values by "," and storing it in an array.And then I want to compare the first value at the array in this case 'Car' to available classes, if it matches to the car class it would pass values to its constructor or else if its bike it would pass values to bike class.

Deepika
  • 1
  • 1
  • I'm not sure what exactly your question is getting at. Do you need help reading the text file? – Flaom Apr 07 '18 at 22:31
  • Hey, I am able to read from a file, My question is if its possible to compare a string to a class name??Like I am reading Bike from the text file and I have a class by the same name then constructor of Bike class is called – Deepika Apr 07 '18 at 22:38
  • Try using `getClass().getName()`. – Flaom Apr 07 '18 at 22:41
  • https://stackoverflow.com/questions/6271417/java-get-the-current-class-name – Flaom Apr 07 '18 at 22:41
  • The answer is yes, but you propably want to have a different question. – Nikolaus Apr 07 '18 at 22:47

1 Answers1

0

You would need to in your main class create an array of type Object and another String array with each value in the string array the name of each Object in the Object array using Object.getname(). Then compare your String array containing your test file with that containing the Object names, and from there on use selection to pass values to constructors. If you post some code, it would help people to understand your question better.

Raymo111
  • 514
  • 8
  • 24