简体   繁体   中英

Java - read file into array of objects

I am in need of some guidance. I am not sure how to go about reading in the sample text file into an array of objects. I know that the work needs to be in the while loop I have in main. I just do not know what I need to accomplish this.

I understand that I need to read in the file line by line (that's what the while loop is doing), but I don't know how to parse that line into an object in my array.

I know all of you like to see what people have tried before you help, but I honestly don't know what to try. I don't need a hand out, just some guidance.

Sample Text File:

100 3
120 5
646 7
224 9
761 4

Main:

public static void main(String[] args) {

    Weight[] arrWeights = new Weight[25];
    int count = 0;

    JFileChooser jfc = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());

    int returnValue = jfc.showOpenDialog(null);

    if (returnValue == JFileChooser.APPROVE_OPTION) {
        File selectedFile = jfc.getSelectedFile();
        System.out.println(selectedFile.getAbsolutePath());

        BufferedReader inputStream = null;
        String fileLine;

        inputStream = new BufferedReader(new FileReader(selectedFile.getAbsoluteFile()));

        System.out.println("Weights:");

        // Read one Line using BufferedReader
        while ((fileLine = inputStream.readLine()) != null) {
            count++;
            System.out.println(fileLine);
        }

        System.out.println("Total entries: " + count);
    }
}

Weight Class:

public class Weight {

    private int pounds;
    private double ounces;
    private final int OUNCES_IN_POUNDS = 16;

    public Weight(int pounds, double ounces) {
        this.pounds = pounds;
        this.ounces = ounces;
    }

    public boolean lessThan(Weight weight) {
        return toOunces() < weight.toOunces();
    }

    public void addTo(Weight weight) {
        this.ounces += weight.toOunces();
        normalize();
    }

    public void divide(int divisor) {
        if (divisor != 0) {
            this.ounces = (this.toOunces() / divisor);
            this.pounds = 0;
            normalize();
        }
    }

    public String toString() {
        return this.pounds + " lbs " + String.format("%.3f", this.ounces) + " oz";
    }

    private double toOunces() {
        return this.pounds * OUNCES_IN_POUNDS + this.ounces;
    }

    private void normalize() {
        if (ounces >=16) {
            this.pounds += (int) (this.ounces /OUNCES_IN_POUNDS);
            this.ounces = this.ounces % OUNCES_IN_POUNDS;
        }
    }
}

I don't remember how to do that exactly in Java but I think this general guidance could help you:

In the while loop -

  1. Parse the line you input from the read line using split function, you can use this as reference( first example can do the trick ): http://pages.cs.wisc.edu/~hasti/cs302/examples/Parsing/parseString.html

  2. Take the parsed line values, cast them to desired values per your class and create your object.

  3. Append the created object to your list of objects: arrWeights

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM