2d Arrays trying to get same value in if function JAVA

zeniya :

Hi I am near the end of my assignment, but can't seem to get my output values to output large cities in Canada per province that are also the capital city of said province. Below is my code and in a separate box is my method code. Thanks in advance.

public class Canada
{
    public static final int BC = 0;
    public static final int AB = 1;
    public static final int ON = 2;
    public static final int QC = 3;
    public static final int NL = 4;
    public static final int PE = 5;
    public static final int NS = 6;
    public static final int NB = 7;
    public static final int MB = 8;
    public static final int SK = 9;
    public static final int NU = 10;
    public static final int YT = 11;
    public static final int NT = 12;

    public static final int NAME_OF_PROVINCE = 0;
    public static final int NAME_OF_CAPITAL_CITY = 1;
    public static final int NAME_OF_BIGGEST_CITY = 2;

    public String[][] provinces;
    public Canada()
    {
        provinces = new String[13][3];

        provinces[BC][NAME_OF_PROVINCE] = "british columbia";
        provinces[BC][NAME_OF_CAPITAL_CITY] ="victoria";
        provinces[BC][NAME_OF_BIGGEST_CITY] ="vancouver";

        provinces[AB][NAME_OF_PROVINCE] = "alberta";
        provinces[AB][NAME_OF_CAPITAL_CITY] ="edmonton";
        provinces[AB][NAME_OF_BIGGEST_CITY] ="calgary";

        provinces[ON][NAME_OF_PROVINCE] = "ontario";
        provinces[ON][NAME_OF_CAPITAL_CITY] ="toronto";
        provinces[ON][NAME_OF_BIGGEST_CITY] ="toronto";

        provinces[QC][NAME_OF_PROVINCE] = "quebec";
        provinces[QC][NAME_OF_CAPITAL_CITY] ="quebec city";
        provinces[QC][NAME_OF_BIGGEST_CITY] ="montreal";

        provinces[NL][NAME_OF_PROVINCE] = "newfoundland";
        provinces[NL][NAME_OF_CAPITAL_CITY] ="st johns";
        provinces[NL][NAME_OF_BIGGEST_CITY] ="st johns";

        provinces[PE][NAME_OF_PROVINCE] = "prince edward island";
        provinces[PE][NAME_OF_CAPITAL_CITY] ="charlottetown";
        provinces[PE][NAME_OF_BIGGEST_CITY] ="charlottetown";

        provinces[NS][NAME_OF_PROVINCE] = "nova scotia";
        provinces[NS][NAME_OF_CAPITAL_CITY] ="halifax";
        provinces[NS][NAME_OF_BIGGEST_CITY] ="halifax";

        provinces[NB][NAME_OF_PROVINCE] = "new brunswick";
        provinces[NB][NAME_OF_CAPITAL_CITY] ="fredericton";
        provinces[NB][NAME_OF_BIGGEST_CITY] ="saint john";

        provinces[MB][NAME_OF_PROVINCE] = "manitoba";
        provinces[MB][NAME_OF_CAPITAL_CITY] ="winnipeg";
        provinces[MB][NAME_OF_BIGGEST_CITY] ="winnipeg";

        provinces[SK][NAME_OF_PROVINCE] = "saskatchewan";
        provinces[SK][NAME_OF_CAPITAL_CITY] ="regina";
        provinces[SK][NAME_OF_BIGGEST_CITY] ="saskatoon";

        provinces[NU][NAME_OF_PROVINCE] = "nunavut";
        provinces[NU][NAME_OF_CAPITAL_CITY] ="iqaluit";
        provinces[NU][NAME_OF_BIGGEST_CITY] ="iqaluit";

        provinces[YT][NAME_OF_PROVINCE] = "yukon";
        provinces[YT][NAME_OF_CAPITAL_CITY] ="whitehorse";
        provinces[YT][NAME_OF_BIGGEST_CITY] ="whitehorse";

        provinces[NT][NAME_OF_PROVINCE] = "northwest territories";
        provinces[NT][NAME_OF_CAPITAL_CITY] ="yellowknife";
        provinces[NT][NAME_OF_BIGGEST_CITY] ="yellowknife";
    }
}

Below here is my method code that I am attempting to pull output with same largecity in province as capital city in province. Like Toronto is that capital city of province Ontario and is also the largest city in Ontario. Whereas, Vancouver is the largest city in BC, but is not the capital city so null. I have tried a wide variety of combinations where it would either always output not computing or it would just output the city I type.

p.s. I know the 2d array code is atrocious, but it was the way the teacher set up the initial build of this question.

public String getProvinceWhoseCapitalsIs(String city) // return
{
    String sameCity = "not computing";
    for(int i = 0;i<provinces.length;i++)
    {
        if(city == null || city.isEmpty())
        {
            throw new IllegalArgumentException("Please enter correct province name");
        }
        if(city == provinces[i][1] && city == provinces[i][2])
        {
            city = provinces[i][1];

        }
        break;
    }
    return city;
}

Made some edits to the method and now it is only returning what I type as output...

Arvind Kumar Avinash :

There are three problems in your code:

  1. You are using city == provinces[i][1] and city == provinces[i][2] to compare the strings which is not the correct way of comparing strings. Note that == compares just the references; not the content of the strings.
  2. You have put the break statement outside the if block (which is expected to evaluate true when the matching city is found). Because of this, the for loop is getting executed only once irrespective of the number of elements in the array, provinces and whether or not a match is found.
  3. If I understood the purpose of getProvinceWhoseCapitalsIs correctly, it is supposed to return the name of the province and not the city. Therefore, you should return provinces[i][0] instead of provinces[i][1].

You can ignore the 3rd point mentioned above if it is not what your method is intended to do.

Do it as follows:

public String getProvinceWhoseCapitalsIs(String city) {
    String province = "";
    if (city == null || city.isEmpty()) {
        throw new IllegalArgumentException("Please enter correct province name");
    }
    for (int i = 0; i < provinces.length; i++) {
        if (city.equalsIgnoreCase(provinces[i][1])) {
            province = provinces[i][0];
            break;
        }
    }
    return province;
}

[Update]

Posting this update to write the following test code:

public class Main {
    public static void main(String[] argv) {
        Canada canada = new Canada();
        System.out.println(canada.getProvinceWhoseCapitalsIs("toronto"));
    }
}

Output:

ontario

[Another update]

Based on your comment, I understood that you are looking for a method to get the list of the biggest capital cities. In order to meet this requirement, you need to put the following method into the class Canada:

public List<String> getBiggestCapitalCities() {
    List<String> cities = new ArrayList<String>();
    for (int i = 0; i < provinces.length; i++) {
        if (provinces[i][1].equalsIgnoreCase(provinces[i][2])) {
            cities.add(provinces[i][1]);
        }
    }
    return cities;
}

Test Code:

public class Main {
    public static void main(String[] argv) {
        Canada canada = new Canada();
        System.out.println(canada.getBiggestCapitalCities());
    }
}

Output:

[toronto, st johns, charlottetown, halifax, winnipeg, iqaluit, whitehorse, yellowknife]

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=284713&siteId=1