Java: Taking file contents into a vector
<p>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!</p>

<p>This is my code:</p>

<pre><code>public class Program5 {
public static void main(String[] args) throws Exception
       {
           Vector&lt;Product&gt; productList = new Vector&lt;Product&gt;();

           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&lt;productList.size();j++)             
               {                                
                   Product aProduct = productList.get(j);

                   System.out.println(aProduct.code+", "+aProduct.desc+", "+aProduct.price+" "+aProduct.unit+" ");                  
               }   

           }

       }
</code></pre>

<p>}</p>

<p>And this is the content of the file I'm trying to read in and what it should print from my code:</p>

<p>K3876, Distilled Moonbeams, $3.00, a dozen<br>
P3487, Condensed Powdered water, $2.50, per packet<br>
Z9983, Anti-Gravity Pills, $12.75, for 60<br></p>

<p>But this is what i got from running the code:</p>

<p>K3876, Distilled Moonbeams, $3.00 a dozen<br>
K3876, Distilled Moonbeams, $3.00 a dozen<br>
P3487, Condensed Powdered water, $2.50 per packet<br>
K3876, Distilled Moonbeams, $3.00 a dozen<br>
P3487, Condensed Powdered water, $2.50 per packet<br>
Z9983, Anti-Gravity Pills, $12.75 for 60<br></p>
