简体   繁体   中英

How do I traverse to the next position of an array

I have a constructor which accepts a line of text such as "2 Street|Spain|Jamaica".

public Address(String lines) {
        arrOfStr = lines.split("[|]");

        streetName = arrOfStr[0];
        zone = arrOfStr[1];
        countryName = arrOfStr[2];
    }

And a method which gets the Country at the end of this String from the constructor

public String getCountry() {
    return countryName;
}

With a given input such as "2 Street|Spain||Jamaica" I would want to get the country name but the output only returns a blank space.

This was my attempt at getting the Country at the end of the String by making alterations to the constructor

public Address(String lines) {
        arrOfStr = lines.split("[|]");

        streetName = arrOfStr[0];
        zone = arrOfStr[1];
        if (countryName != " ") {
            countryName = arrOfStr[2];
        } else {
            countryName = arrOfStr[3];
        }

    }

Any assistance on how to get the country name?

If I understand correctly the last part of String is what countryName is. Would countryName = arrOfStr[arrOfStr.length - 1] be of help?

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