Java – read data files into object arrays?
•
Java
I encountered a problem reading files into an array of objects I created an IF statement to divide the data rows into two different subgroups, one generated and the other cleaned But when I run the program, the created object is empty How do I connect files to objects? I missed 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()); } } }
Solution
Your problem is that you don't even set variables in your objects. All you do is make them produce and clean, but don't fill their fields
If I don't know how to set up your product, product and cleaning classes and how to fill their variables, I can't answer further
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码