简体   繁体   中英

Is it possible to create a list in java using data from multiple text files

I have multiple text files that contains information about different programming languages popularity in different countries based off of google searches. I have one text file for each year from 2004 to 2015. I also have a text file that breaks this down into each week (called iot.txt) but this file does not include the country.

Example data from 2004.txt:

Region  java    c++ c#  python  JavaScript

Argentina   13  14  10  0   17

Australia   22  20  22  64  26

Austria 23  21  19  31  21

Belgium 20  14  17  34  25

Bolivia 25  0   0   0   0

etc

example from iot.txt:

Week    java    c++ c#  python  JavaScript

2004-01-04 - 2004-01-10 88  23  12  8   34

2004-01-11 - 2004-01-17 88  25  12  8   36

2004-01-18 - 2004-01-24 91  24  12  8   36

2004-01-25 - 2004-01-31 88  26  11  7   36

2004-02-01 - 2004-02-07 93  26  12  7   37

My problem is that i am trying to write code that will output the number of countries that have exhibited 0 interest in python.

This is my current code that I use to read the text files. But I'm not sure of the best way to tell the number of regions that have 0 interest in python across all the years 2004-2015. At first I thought the best way would be to create a list from all the text files not including iot.txt and then search that for any entries that have 0 interest in python but I have no idea how to do that.

Can anyone suggest a way to do this?

import java.io.BufferedReader;
import java.io.FileReader;

import java.util.*;

public class Starter{

    public static void main(String[] args) throws Exception {
        BufferedReader fh =
                new BufferedReader(new FileReader("iot.txt"));
        //First line contains the language names
        String s = fh.readLine(); 
        List<String> langs =
                new ArrayList<>(Arrays.asList(s.split("\t")));
        langs.remove(0);    //Throw away the first word - "week"
        Map<String,HashMap<String,Integer>> iot = new TreeMap<>();
        while ((s=fh.readLine())!=null)
        {
            String [] wrds = s.split("\t");
            HashMap<String,Integer> interest = new HashMap<>();
            for(int i=0;i<langs.size();i++)
                interest.put(langs.get(i), Integer.parseInt(wrds[i+1]));
            iot.put(wrds[0], interest);
        }
        fh.close();
        HashMap<Integer,HashMap<String,HashMap<String,Integer>>>
            regionsByYear = new HashMap<>();
        for (int i=2004;i<2016;i++)
        {
            BufferedReader fh1 =
                    new BufferedReader(new FileReader(i+".txt"));
            String s1 = fh1.readLine(); //Throw away the first line
            HashMap<String,HashMap<String,Integer>> year = new HashMap<>();
            while ((s1=fh1.readLine())!=null)
            {
                String [] wrds = s1.split("\t");
                HashMap<String,Integer>langMap = new HashMap<>();
                for(int j=1;j<wrds.length;j++){
                    langMap.put(langs.get(j-1), Integer.parseInt(wrds[j]));
                }
                year.put(wrds[0],langMap);
            }
            regionsByYear.put(i,year);
            fh1.close();
        }
    }
}

Create a Map<String, Integer> using a HashMap and each time you find a new country while scanning the incoming data add it into the map country->0. Each time you find a usage of python increment the value.

At the end loop through the entrySet of the map and for each case where e.value() is zero output e.key() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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