Home > Software design >  How to store info from an Array to an Array Object?
How to store info from an Array to an Array Object?

Time:02-11

I have a Java class which uses BufferedReader to obtain information from a text file, then store the information into an Array called newData . I want to store a certain part of the information to the VegTypes[f] = new VegType(); but I not sure what code should I write here to obtain that part of information.

Without completing this part, I am not able to continue working on another Array Object which is Vegs[i] = new Veg(newData[0], newData[1], newData[2],); for storing information together with VegTypes Array Object.

Below is my code of the Java class:

public class theVegetable {

    private Veg[] Vegs;
    private VegType[] VegTypes;

    public theVegetable() {
        int quantity;
        int vegQuantity;
        String vegLine;

        try {

            BufferedReader br = new BufferedReader(new FileReader("vegetableInfo.txt"));
            quantity = Integer.parseInt(vegLine.readLine());

            Vegs = new Veg[quantity];

            for (int i = 0; i < quantity; i  ) {
               vegLine = br.readLine();
                String[] newData = vegLine.split(";");

                vegQuantity = Integer.parseInt(newData[3]);


                //For loop to store information into VegTypes
                for (int f = 0; j < vegQuantity; f  ) {
                    
                    VegTypes[f] = new VegType();
                    
                }
                
                //Vegs Array Object to store information plus VegTypes
                Vegs[i] = new Veg(newData[0], newData[1], newData[2],);

            }

            br.close();

        } catch (FileNotFoundException e) {
            System.out.println("File not found.");
        }
    }
}

Below is my vegetableInfo.txt text file:

3
Tomato;class1;Malaysia Tomato;2;MT100A;MT1;200;90;MT20A;MT2;600;80;Malaysia product
Avocado;class2;Europe Avocado;4;EA100A;EA1;300;90;EA14A;EA2;90;80;EA230A;EA3;43;50.9;EA470A;EA4;400;76;Europe product
Cabbage;class3;Malaysia Cabbage;3;MC100A;MC1;500;20;MC49A;MC2;500;50;MC800A;MC3;600;10.3;Malaysia product

The number 3 at the top of the text file is for the int quantity; variable to store the amount. The kind of information I want the VegTypes[f] = new VegType(); to store are MT100A;MT1;200;90;MT20A;MT2;600;80;, the number 2 besides the Malaysia Tomato are for int vegQuantity; variable. Same thing goes for other vegetables in the text file.

Constructor of my private VegType[] VegTypes; Array Object:

private String vegCode;
private String vegBatch;
private int vegBatchQuantity;
private double vegGrade;

    public VegType(String inVegCode, String inVegBatch, int inVegBatchQuantity, double inVegGrade) {
        vegCode = inVegCode;
        vegBatch = inVegBatch;
        vegBatchQuantity = inVegBatchQuantity;
        vegGrade = inVegGrade;
    }

My Veg Class:

public class Veg {

    private String vegetableName;
    private String classLevel;
    private String productionCountry;
    private VegType[] VegTypes;
    private String productType;

    //Constructor
    public Veg(String inVegetableName, String inClassLevel, String inProductionCountry, VegType[] inVegTypes, String inProductType) {
        vegetableName = inVegetableName;
        classLevel = inClassLevel;
        productionCountry = inProductionCountry;
        vegType = inVegTypes;
        
        productType = inProductType;
    }


    public String getVegetableName() {
        return vegetableName;
    }

    public String getClassLevel() {
        return classLevel;
    }

    public String getProductionCountry() {
        return productionCountry;
    }

    public String getProductType() {
        return productType;
    }
}

CodePudding user response:

This is wrong:

                //For loop to store information into VegTypes
                for (int f = 0; j < vegQuantity; f  ) {
                    
                    VegTypes[f] = new VegType();
                    
                }

you need to use f to adjust the index into your array of fields.

                for (int f = 0; j < vegQuantity; f  ) {
                    String vegCode = newLine[f*4   4];
                    String vegBatch = newLine[f*4   5];
                    int vegQuantity = Integer.parse(newLine[f*4   6]);
                    double vegGrade = Double.parse(newLine[f*4   7]);

                    VegTypes[f] = new VegType(vegCode, vegBatch, vegQuantity, vegGrade);
                    
                }

CodePudding user response:

after //For loop to store information into VegTypes line add below,

for(int j=4; j< newData.length-1; j =4) {
    VegTypes[f] = new VegType(newData[j], newData[j 1], Integer.parseInt(newData[j 2]), Double.parseDouble(newData[j 3]));
}

Simply you're iterating from 4th index to the end-1 index. Each iteration you're considering as 4 element windows.

CodePudding user response:

I think that you are looking for ArrayList

ArrayList<vegType> myVeges = new ArrayList<vegType>();
VegType carrots = new VegType("carrots","batchRef",25,1.0);
myVeges.add(carrots);
  • Related