在java

时间:2016-04-21 23:26:00

标签: java java-io parallel-arrays

这是我从文件中读取并在控制台中显示的代码

    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();

在我的名为“sales.txt”的文件中,我有这个:

2012,Q1,9300

2012,Q2,10225

2012,Q3,12420

2012,Q4,13250

2013,Q1,10500

2013,Q2,10900

2013,Q3,11340

2013,Q4,14600

现在我被困在如何计算2012年和2013年第四季度的平均销售额

3 个答案:

答案 0 :(得分:0)

就这种情况而言:

     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();

答案 1 :(得分: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()]);

答案 2 :(得分:0)

  

//或者从文件中读取数组

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++;
        }