i'm trying to read in the file contents and put them in a vector and print it out but I'm having some issues where it prints the contents repeatedly! Please help to see what's wrong with my code! Thank you!
This is my code:
public class Program5 {
public static void main(String[] args) throws Exception
{
Vector<Product> productList = new Vector<Product>();
FileReader fr = new FileReader("Catalog.txt");
Scanner in = new Scanner(fr);
while(in.hasNextLine())
{
String data = in.nextLine();
String[] result = data.split("\\, ");
String code = result[0];
String desc = result[1];
String price = result[2];
String unit = result[3];
Product a = new Product(desc, code, price, unit);
productList.add(a);
for(int j=0;j<productList.size();j++)
{
Product aProduct = productList.get(j);
System.out.println(aProduct.code+", "+aProduct.desc+", "+aProduct.price+" "+aProduct.unit+" ");
}
}
}
}
And this is the content of the file I'm trying to read in and what it should print from my code:
K3876, Distilled Moonbeams, $3.00, a dozen
P3487, Condensed Powdered water, $2.50, per packet
Z9983, Anti-Gravity Pills, $12.75, for 60
But this is what i got from running the code:
K3876, Distilled Moonbeams, $3.00 a dozen
K3876, Distilled Moonbeams, $3.00 a dozen
P3487, Condensed Powdered water, $2.50 per packet
K3876, Distilled Moonbeams, $3.00 a dozen
P3487, Condensed Powdered water, $2.50 per packet
Z9983, Anti-Gravity Pills, $12.75 for 60