1

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

zestncb
  • 13
  • 4
  • 1
    Move your for loop outside the while loop. – Rohit Jain Feb 15 '13 at 10:20
  • 1
    Don't use a Vector. It's been [obsolete for quite some time](http://stackoverflow.com/questions/1386275/why-is-java-vector-class-considered-obsolete-or-deprecated) now. Use an [ArrayList](https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html) or similar instead. – ManoDestra Jul 27 '16 at 19:34

3 Answers3

0

move the for-loop outside while .

//outside while

for(int j=0;j<productList.size();j++)             
               {                               
               Product aProduct = productList.get(j);    
                   System.out.println(aProduct.code+", "+aProduct.desc+", "+aProduct.price+" "+aProduct.unit+" ");                  
               }  

BTW, never use Vector unless you care about thread safety. Vector's methods are synchronized use ArrayList (which is quite efficient and fast) if you dont care about thread safety

PermGenError
  • 45,977
  • 8
  • 87
  • 106
  • Eh, even worrying about thread safety I wouldn't use Vector as opposed to either a lock or a different synchronized collection/`Collections#synchronizedList` – Rogue Jul 31 '16 at 23:27
0

Put the for-loop out side of while loop. nested for-loop prints redundant data.

Vector<Product> productList = new Vector<Product>();
...
while(in.hasNextLine()){
   ...
   productList.add(a);
}
for(int j=0;j<productList.size();j++){
   ....
}
Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
0

em,you can try to move "System.out.println(...)" out of the "for" loop:

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+" ");

}
Tong.Z
  • 1
  • 3