-2

This is my code to read from file and 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 my file named "sales.txt" I 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 I am stuck on how to calculate average sales for q4 in year 2012 and 2013

shmosel
  • 49,289
  • 6
  • 73
  • 138
Konstantin F
  • 180
  • 1
  • 15
  • Have you learned about variables yet? – shmosel Apr 21 '16 at 23:29
  • I'm sorry I don't get the question – Konstantin F Apr 21 '16 at 23:43
  • Calculating an average is a very elementary task. If you tried to do it but you have a *specific* problem, post your code and explain what's wrong. If you have no idea what to do, I'm not sure you're in the right place. – shmosel Apr 21 '16 at 23:46
  • oh you mean that ,yes I know how to calculate average,but how to do it in this situation?how to display at least q4 in 2012 and 2013? – Konstantin F Apr 21 '16 at 23:49

3 Answers3

0

Just for this 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 is the average! in avg
            readFile.close();
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
        userInput.close();
granmirupa
  • 2,780
  • 16
  • 27
0
//Read from file and store data in Array List
    Scanner inputFile = new Scanner(new File("sales.txt"));

    ArrayList<Integer> yearsList = new ArrayList<Integer>();
    ArrayList<String> quartersList = new ArrayList<String>();
    ArrayList<Integer> salesList = new ArrayList<Integer>();

    while(inputFile.hasNextLine()){
        String line = inputFile.nextLine();

        Scanner scanner = new Scanner(line);
        scanner.useDelimiter(",");
        while(scanner.hasNextLine()){
            yearsList.add(scanner.nextInt());
            quartersList.add(scanner.next());
            salesList.add(scanner.nextInt());
        }
        scanner.close();
    }

    inputFile.close();

    //. Testing Can I read the file properly 
    // System.out.println(years+" \n" + quarters + " \n" + sales);

    //. Convert from ArrayList into Array 
    Integer[]yearsArray = yearsList.toArray(new Integer[yearsList.size()]); 
    String[] quartersArray = quartersList.toArray(new String[quartersList.size()]);
    Integer[]salesArray = salesList.toArray(new Integer[salesList.size()]);
Konstantin F
  • 180
  • 1
  • 15
0

//Or read as Arrays from file

int[] year = new int [lineCount];
        String[] quarter = new String [lineCount];
        int[] sale = new int [lineCount];

        Scanner readFile = new Scanner(new File("sales.txt"));

        while(readFile.hasNextLine())
        {
            String salesRecords = readFile.nextLine();
            Scanner lineScan = new Scanner(salesRecords);
            lineScan.useDelimiter(",");
            year[i] = lineScan.nextInt();
            quarter[i] = lineScan.next();
            sale[i] = lineScan.nextInt();
            lineScan.close();
            i++;
        }
Konstantin F
  • 180
  • 1
  • 15