read from file as parallel arrays in java -


this code read file , display in console

    try     {         bufferedreader readfile = new bufferedreader(new filereader("sales.txt"));         string line = "";         while((line = readfile.readline()) != null)         {             string tmp[] = line.split(",");             year = integer.parseint(tmp[0]);             quarter = tmp[1];             sales = integer.parseint(tmp[2]);             //system.out.printf("year: %s\tquarter: %s\tsales: %d\n",year,quarter,sales);            }         readfile.close();     }     catch(ioexception e)     {         e.printstacktrace();     }     userinput.close(); 

in file named "sales.txt" have this:

2012,q1,9300

2012,q2,10225

2012,q3,12420

2012,q4,13250

2013,q1,10500

2013,q2,10900

2013,q3,11340

2013,q4,14600

now stuck on how calculate average sales q4 in year 2012 , 2013

just case:

     float avg = 0;      int counter = 0;      try         {             bufferedreader readfile = new bufferedreader(new filereader("sales.txt"));             string line = "";             while((line = readfile.readline()) != null)             {                 string tmp[] = line.split(",");                 year = integer.parseint(tmp[0]);                 quarter = tmp[1];                 sales = integer.parseint(tmp[2]);                 if(year == 2012 || year == 2013)                    if(quarter.equals("q4"){                        counter++;                        avg+=sales;                    }                 //system.out.printf("year: %s\tquarter: %s\tsales: %d\n",year,quarter,sales);                }             avg /= counter; //here there average! in avg             readfile.close();         }         catch(ioexception e)         {             e.printstacktrace();         }         userinput.close(); 

Comments

Popular posts from this blog

Django REST Framework perform_create: You cannot call `.save()` after accessing `serializer.data` -

Why does Go error when trying to marshal this JSON? -