Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading in a data file into an array of objects?

Tags:

java

object

I'm having problems reading in my file into an array of objects. I created an if statement so that the lines of data get separated into two different subgroups one is produce and the other is cleaning. But when I run the program the objects that are created are empty. How do I connect the file into the objects? I'm missing something crucial.

import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;

public class Inventory{

    public static void main(String[] args){
         int i=0;
         Product[] pr=new Product[16];
         File InventoryFile=new File("inventory.csv");
         Scanner in=null;
         try{
            in=new Scanner(InventoryFile);
            while(in.hasNext()){
               String line=in.nextLine();
               String[]fields=line.split(",");
               if(fields[0].equals("produce"))
                    pr[i]= new Produce();
               else 
                    pr[i]=new Cleaning();
               i++;
            }
            System.out.println(pr[6]);  
           }catch(FileNotFoundException e){
             System.out.println("Arrgggg"+e.getMessage());
           }    
      }
  }
like image 310
Ivan_Stepul Avatar asked Oct 21 '22 05:10

Ivan_Stepul


1 Answers

Your problem stems from not even setting your varibles in your objects all you haven been doing is making them produce and cleaning but not filled in their fields.

I can not answer further without knowing how you set up your produce, product, and cleaning classes and how they get their varibles filled.

like image 100
Eric Eugene Herring Avatar answered Oct 27 '22 10:10

Eric Eugene Herring