1

I don't really understand the logic behind this question and as a result, I can't really accurately search whether something like this already exists.

Let's say I create a Java class for an object. It only consists of instance variables, getters/setters, and a constructor to create an empty object. For example:

public class Bicycle {
    private int speed;
    private String model;

    public Bicycle(int speed, String model) {
    this.speed = speed;
    this.model = model;
    }

    public int getSpeed() {
        return speed;
    }

    public void setSpeed() {
        this.speed = speed;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }
}

And now let's say I had a text file of bicycles like:

Bike 1234, 60

Bike 333, 50

Bike 555, 20

How would I read the file and then assign each part of the text file to the variables in Bicycle.java?

I want to be able to use the getters and setters in other methods. For example:

public static String searchBikes(int speed, String model) {
    if (Bicycle bike.getSpeed().equals(speed) && etc. etc.

I'm unsure as to how to assign each bicycle in the text file to the variables in Bicycle class. Do I need another class (like a database) of sorts? I'm just very lost on this.

2
  • You need to read about Java Serializable Objects Commented Nov 16, 2015 at 14:27
  • One approach could be to read the file, iterate over each line of the file, parse the values from each line, use those values to create an instance of your object, and add that instance to a collection of instances. Commented Nov 16, 2015 at 14:31

3 Answers 3

1
Scanner fileScan = new Scanner(new File("yourfile.txt"));
List<Bicycle> bikes = new ArrayList<>();
while(fileScan.hasNextLine()){
    String[] line = fileScan.nextLine().split(",");
    Bicycle bike = new Bicycle(line[1],line[0]);
    bikes.add(bike);
}

Bikes in your file are now in bikes list.

Sign up to request clarification or add additional context in comments.

Comments

0

The best approach now a days is java serialization, you can define a human-readable data structure for the bikes and try to convert it to a POJO (plain old java objects), one of the options is using Json format, it es easy t read and there are many APIs for converting strings (son objects to POJO) Gson, Jackson etc etc

a json representation of your file can b:

{
  "speed": 33,
  "model": 50
}

and with this infomation you can get instances of a class Bicycles, where the attributes model and speed are available as in a "Normal" java object.

take a look at this and this tutorials...

Comments

0

You should read more on "classes and objects" to fully understand what is a CLASS and what is an instance of a class - object.

Oracle's web site can be a good start https://docs.oracle.com/javase/tutorial/java/concepts/

Concerning your concrete problem, well since we talk about object oriented programming, why not just copy the real world model ? Where are bicycles made - in a factory, using a specification of some sort, let's call it a blueprint.

You can create a class called BycicleFactory with a method called "createBycicles(File blueprints)" that returns an ArrayList for example. Here is some more info about the design pattern (best practice) called Factory Design Pattern http://www.tutorialspoint.com/design_pattern/factory_pattern.htm

Inside your factory, you will have to read the file (the blueprint) and create a new Bycicle object per line. I suggest you youse a Scanner, since it is the easiest. http://www.java2s.com/Code/JavaAPI/java.util/newScannerFileReaderfile.htm

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.