简体   繁体   English

在java中作为并行数组从文件中读取

[英]read from file as parallel arrays in java

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:在我名为“sales.txt”的文件中,我有这个:

2012,Q1,9300 2012年一季度9300

2012,Q2,10225 2012年第二季度10225

2012,Q3,12420 2012年第三季度12420

2012,Q4,13250 2012年第4季度13250

2013,Q1,10500 2013年一季度10500

2013,Q2,10900 2013年第2季度10900

2013,Q3,11340 2013年第三季度11340

2013,Q4,14600 2013年第4季度14600

Now I am stuck on how to calculate average sales for q4 in year 2012 and 2013现在我被困在如何计算 2012 年和 2013 年第四季度的平均销售额

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM