1

enter image description hereI read lines from a CSV file and imported those to the ArrayList but comes out as below. This is considered as index 0 . How do I fix this into multiple index? (I.E [Asia, Asia, Asia, Africa, Africa...]). empty[10] is a column position in excel sheet.

    public class COVIDDataAnalyzer
{
    // You will need to define attributes to manage the data here!
    ArrayList<COVIDData> covidDataList = new ArrayList<COVIDData>();
    Map<String, Integer> continentByCases = new HashMap<String, Integer>();
    
    /**
     * Read the data in the know WHO format from the specified file
     * @param filename the name of the file to read
     * @return a list of COVIDData objects read from the file
     * @throws ParseException 
     */
    public List<COVIDData> readFile(String filename) throws IOException, ParseException
    {
        try (Scanner input = new Scanner(new File(filename));){
            if (input.hasNext() == true) {
                input.nextLine();
                while (input.hasNextLine()) {
                    covidDataList.add(getRecordFromLine(input.nextLine()));
                }
            } else {
                return null;
            }
        }
        return covidDataList;
    }
    
    private COVIDData getRecordFromLine(String line) throws ParseException{
        
        line.split(",");
        String [] empty = line.split(",");
        
        long cases = Long.parseLong(empty[4]);
        long death = Long.parseLong(empty[5]);
        long population = Long.parseLong(empty[9]);
        int year = Integer.parseInt(empty[3]) - 1900;
        int month = Integer.parseInt(empty[2]) - 1;
        int dates = Integer.parseInt(empty[1]);
        
        List<String> continent = new ArrayList<String>();
        continent.add(empty[10]);
        
        
        
        System.out.println(continent);

        Date date = new Date(year, month, dates);
        
        COVIDData data = new COVIDData(date, cases, death, empty[6], population, empty[10]);
        return data;


    }
public class COVIDData
{
    private Date day;
    private long cases;
    private long deaths;
    private String country;
    private String continent;
    private long population;

    public COVIDData(Date day, long cases, long deaths, String country, long population, String continent)
    {
        super();
        this.day = day;
        this.cases = cases;
        this.deaths = deaths;
        this.country = country;
        this.population = population;
        this.continent = continent;
    }

    public Date getDay()
    {
        return day;
    }

    public long getCases()
    {
        return cases;
    }

    public long getDeaths()
    {
        return deaths;
    }

    public String getCountry()
    {
        return country;
    }

    public long getPopulation()
    {
        return population;
    }

    public String getContinent()
    {
        return continent;
    }

    @Override
    public String toString()
    {
        return "COVIDData [day=" + day + ", cases=" + cases + ", deaths=" + deaths + ", country=" + country
                + ", continent=" + continent + ", population=" + population + "]";
    }

    @Override
    public int hashCode()
    {
        final int prime = 31;
        int result = 1;
        result = prime * result + (int) (cases ^ (cases >>> 32));
        result = prime * result + ((continent == null) ? 0 : continent.hashCode());
        result = prime * result + ((country == null) ? 0 : country.hashCode());
        result = prime * result + ((day == null) ? 0 : day.hashCode());
        result = prime * result + (int) (deaths ^ (deaths >>> 32));
        result = prime * result + (int) (population ^ (population >>> 32));
        return result;
    }

    @Override
    public boolean equals(Object obj)
    {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        COVIDData other = (COVIDData) obj;
        if (cases != other.cases)
            return false;
        if (continent == null)
        {
            if (other.continent != null)
                return false;
        } else if (!continent.equals(other.continent))
            return false;
        if (country == null)
        {
            if (other.country != null)
                return false;
        } else if (!country.equals(other.country))
            return false;
        if (day == null)
        {
            if (other.day != null)
                return false;
        } else if (!day.equals(other.day))
            return false;
        if (deaths != other.deaths)
            return false;
        if (population != other.population)
            return false;
        return true;
    }
}

Console Result
[Asia]
[Asia]
[Asia]
[Asia]
[Asia]
[Asia]
[Asia]
[Asia]
[Asia]
[Asia]
[Asia]
[Asia]
[Asia]
[Africa]
[Africa]
[Africa]
[Africa]
[Africa]
[Africa]
[Africa]
[Africa]
[Africa]
[Africa]
[Africa]

Thx.

7
  • 2
    Sorry, it's not entirely clear what you're asking. What do you mean by "how do I fix this into multiple index"? Commented May 27, 2021 at 2:22
  • 3
    What does a CSV record look like? How are you reading the CSV file? What is empty? How are you printing the result? --- In short, please provide something closer to a Minimal, Reproducible Example. You should also read "How do I ask a good question?" before fixing the question. Commented May 27, 2021 at 2:58
  • please add the .csv format so that we get a clear idea. Commented May 27, 2021 at 5:31
  • Does the CSV file you are reading have a Header Line? If so, what does it look like? Commented May 27, 2021 at 10:40
  • @Matt U. Sorry for the confusion. English is not my first language and this is the first time using StackOverFlow. So basically I need to have continents without duplicates in order to calculate how many cases are there per continents (There are 5 continents in the CSV file). I read all the lines in to the String list (Ignoring the header line) and added that into ArrayList. But it's coming out as size one index so I cant remove the duplicates in the ArrayList. How do I remove the duplicates? Commented May 27, 2021 at 16:33

1 Answer 1

2

If you don't want to have duplicate in a collection one way is to use a collection that doesn't contains duplicate like Set

So what you can do would looks like :

Set<String> continents = new HashSet<String>();
continents.add(empty[10]);

One other would be to use the distinct function of the stream like :

continents
 .stream()
 .distinct()
 .collect(Collectors.toUnmodifiableList());

But in any case you should not create a new Array each time you read a new row.

An other way to solve your problem is after reading the all line of your csv you could groupBy continent and sum by cases like :

List<COVIDData> covidData = new COVIDDataAnalyzer().readFile(filePath);

Map<String, Long> collect = covidData
 .stream()
 .collect(Collectors.groupingBy(COVIDData::getContinent,
  Collectors.summingLong(COVIDData::getCases)));
Sign up to request clarification or add additional context in comments.

1 Comment

It seems like all the suggestions has the same results that I had before. I'm not sure what I'm doing wrong

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.