如何遍历到数组的下一个位置

huangapple go评论90阅读模式
英文:

How do I traverse to the next position of an array

问题

我有一个接受文本行的构造函数,例如"2 Street|Spain|Jamaica"。

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

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

还有一个从构造函数的字符串末尾获取国家的方法。

public String getCountry() {
    return countryName;
}

对于类似"2 Street|Spain||Jamaica"的给定输入,我希望得到国家名称,但输出只返回一个空格。

这是我尝试通过更改构造函数来获取字符串末尾的国家的方式。

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

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

    }

如何获取国家名称?

英文:

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?

答案1

得分: 0

如果我理解正确,字符串的最后部分是countryName所代表的内容。countryName = arrOfStr[arrOfStr.length - 1] 会有帮助吗?

英文:

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

huangapple
  • 本文由 发表于 2020年4月10日 05:52:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/61130799.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定