英文:
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?
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论