Paweł Koniarski
Paweł Koniarski

Reputation: 157

Creating objects with data from file

I’ve got a text file with data for my application and I want to create objects with that data. Every line in the text file is one object in format: <name> <posX> <posY> <posY> <velX> <velY> <velZ> <mass> <radius>. line is a String with one line from that file. To read the data I split the string on whitespace. Then I get the first element to the variable name.

String[] args = line.split("\\s+");
String name = args[0];

Then I want to take next parameters from the line. I could surely do it like this:

double posX = Double.parseDouble(args[1]);
double posY = Double.parseDouble(args[2]);
double posZ = Double.parseDouble(args[3]);
double velX = Double.parseDouble(args[4]);
double velY = Double.parseDouble(args[5]);
double velZ = Double.parseDouble(args[6]);
double mass = Double.parseDouble(args[7]);
double radius = Double.parseDouble(args[8]);

However, it doesn’t look great. I have thought about doing it like that:

Map<String, Double> params = new HashMap<>();
String[] keys = {"posX", "posY", "posZ", "velX", "velY", "velZ", "mass", "radius"};
int i = 1;
for (String key : keys) {
      params.put(key, Double.parseDouble(args[i]));
      i++;
}

Now I’m not sure if it’s the best possible approach to my problem. Do you know any better way to do it?

Upvotes: 0

Views: 62

Answers (2)

Francisco Romero
Francisco Romero

Reputation: 13199

You can create a new class of Java, in this case Example, in which you will have a constructor (with the same name) like this:

public class Example{

double posX, posY, posZ,velX,velY,velZ,mass,radius;
String name;

   public Example(String line){

   String[] args = line.split("\\s+");

   name = args[0];
   posX = Double.parseDouble(args[1]);
   posY = Double.parseDouble(args[2]);
   posZ = Double.parseDouble(args[3]);
   velX = Double.parseDouble(args[4]);
   velY = Double.parseDouble(args[5]);
   velZ = Double.parseDouble(args[6]);
   mass = Double.parseDouble(args[7]);
   radius = Double.parseDouble(args[8]);
   }

In this class you can have all GET and SET methods for recover information from the object or modify it, respectively. You will have to make each GET and SET method for each variable that you use on your object. For example, if you want to make GET and SET methods for the variable name you have to do like this:

public void setName (String n) {
   this.name = n;
} 

public String getName (){
  return name;
}

And the same for all the variables that you have in your object.

Then, from your Main class of Java you can create a new object like this:

Example object = new Example(string);

where string it's the string that you want to use to create the object.

It will makes your code more clearly.

I expect it will be useful for you.

Upvotes: 1

Flying_Banana
Flying_Banana

Reputation: 2910

It ultimately comes to what you want to do with the data.

If you want to create an instance of a class that stores the data for each entry in the file, the current parsing is probably fine. You can do it with key-value pairs like a hash map (or dictionary, in other languages), but it is slower performance-wise than attributes when you are actually using the objects.

There is nothing wrong with having a lot of lines in a parsing operation, since that is considered boilerplate code anyway (and shouldn't need to be seen again once you define the file formats). Ultimately your way with a loop and a hash map compiles down to roughly the same number of actual instructions executed on your machine, so why put performance over aesthetics for something like this?

Upvotes: 1

Related Questions