user3451158
user3451158

Reputation: 43

Issue adding elements to an ArrayList from a different class

Is it possible to do such a thing? Say I wanted to add the values I gave values to in CountriesTest and add them to the ArrayList in Countries. Also how could I reference aCountries to print for option 2, seeing that I created it inside option 1 I can't access it anywhere else.

Here is my interface

public interface CountriesInterface
{
    public String largestPop();
    public String largestArea();
    public String popDensity();
}

Here is the Countries class

import java.util.*;
public class Countries implements CountriesInterface
{
    private final List<CountriesInterface> theCountries = new ArrayList<>();
    private String cName;
    private String finalPopName;
    private String finalAreaName;
    private String finalDensityName;
    private int cPop = 0;
    private int cArea = 0;
    private int popDensity = 0;
    private int popCounter = 0;
    private int areaCounter = 0;
    private int densityCounter = 0;
    public Countries(String cName, int cPop, int cArea, int popDensity)
    {
        this.cName = cName;
        this.cPop = cPop;
        this.cArea = cArea;
        this.popDensity = popDensity;
    }

    public String largestPop()
    {
        for(int i = 0; i < theCountries.size(); i++)
        {
            if(cPop > popCounter)
            {
                popCounter = cPop;
                finalPopName = cName;
            }    
        }
        return finalPopName;
    }

    public String largestArea()
    {
        for(int i = 0; i < theCountries.size(); i++)
        {
            if(cArea > areaCounter)
            {
                areaCounter = cArea;
                finalAreaName = cName;
            }    
        }
        return finalAreaName;
    } 

    public String popDensity()
    {
        for(int i = 0; i < theCountries.size(); i++)
        {
            if(popDensity > densityCounter)
            {
                densityCounter = popDensity;
                finalDensityName = cName;
            }    
        }
        return finalDensityName;
    } 

}

Here is the CountriesTest class

import java.util.*;
public class CountriesTest
{
    public static void main(String[] args)
        {             
            int population = 0;
            int area = 0;
            int density = 0;
            Scanner myScanner = new Scanner(System.in);            
            boolean done = false;
            do
            {
                System.out.println("1.  Enter a country \n2.  Print countries with the largest population, area, and population density \n3.  Exit");
                int choice = Integer.parseInt(myScanner.nextLine());
                if (choice == 1)
                {
                    System.out.print("Enter name of country: ");
                    String input1 = myScanner.nextLine();
                    System.out.print("Enter area of country in square kilometers: ");
                    String input2 = myScanner.nextLine();
                    population = Integer.parseInt(input2);
                    System.out.print("Enter population of country: ");
                    String input3 = myScanner.nextLine();
                    area = Integer.parseInt(input3);
                    density = population/area;
                    Countries aCountries = new Countries(input1, population, area, density);
                }
                else if(choice == 2)
                {
                    System.out.println("The country with the largest population: " );
                    System.out.println("The country with the largest area: "  );
                    System.out.println("The country with the largest population density is: "  );
                }
                else if(choice == 3)
                {
                     done = true;
                }              
                else
                    System.out.println("Invalid Choice");     
            }
            while (!done);
           System.exit(0);
        }
}

Upvotes: 0

Views: 56

Answers (1)

huelbois
huelbois

Reputation: 7012

OK, there's some mix-up in your code.

  • You are using the "Countries" class at the same time for an individual Country AND the list of countries. I won't recommand it, but at least you should make "static" members and methods which are for the list of countries. Or you could declare List theCountries = new ArrayList<>(); inside the main method instead.

  • You are never adding the new "Countries" object to the list of countries. So, if you've declared theCountries in the main method, just uste "theCountries.add(aCountries)" right after the "new Countries(...)".

  • your seach methods (like largestPop) won't work because they are never searching through the content of the "theCountries" ArrayList. (the "i" variable is just iterating through the indices, but never actually used to get a countent from this ArrayList).

  • and btw, System.exit(0) is not needed (it's implied)

Upvotes: 1

Related Questions