Java对象存在性dilem

Java对象存在性dilem,java,arrays,object,Java,Arrays,Object,我写了以下内容,这是一个国家级的toString,在同一个包中有城市级,而_cities是一个数组,表示我国家内的城市: **已编辑:* public String toString(){ String allCitiesData = ""; //must be initialized for(int i=0;this._cities[i] != null;i++)//run all over the cities until reach the end(null ce

我写了以下内容,这是一个国家级的toString,在同一个包中有城市级,而_cities是一个数组,表示我国家内的城市: **已编辑:*

public String toString(){

    String allCitiesData = "";   //must be initialized 

    for(int i=0;this._cities[i] != null;i++)//run all over the cities until reach the end(null cell)
    {   //concat new city to the string and adds a new line between
        allCitiesData = allCitiesData.concat(this._cities[i].toString()+"\n\n");
    }
    return allCitiesData;
}//toString method  

public String citiesNorthOf(String cityName){
    String allCitiesNorthOf = "";// must be initialized

    for(int i=0; this._cities[i] != null ; i++)
    {
        if (this._cities[i].getCityName() == cityName)
        {
            Point referenceCityCenter = new Point(this._cities[i].getCityCenter());
        }
    }

    for(int i=0; this._cities[i] != null ; i++)//we don't need to exclude the comparable city itself because it will give a false
    {
        if (this._cities[i].getCityCenter().isAbove(referenceCityCenter)) 
        {
            allCitiesNorthOf = allCitiesNorthOf.concat(this._cities[i].toString()+"\n\n");
        }
    }
}//citiesNorthOf method
但是,当我运行它时,它只在这一行显示一个错误:

if (this._cities[i].getCityCenter().isAbove(referenceCityCenter))
Eclipse说:“referenceCityCenter不能解析为变量”。。有什么建议吗


谢谢

您已在代码行不可见的范围内声明了
referenceCityCenter
。尝试在方法开始时声明它(如果它到达验证时为
null
。isAbove()!:P),也要进行控制)

您已在代码行不可见的范围内声明了

referenceCityCenter
。尝试在方法开始时声明它(如果它到达您的验证时为
null
。isAbove()!:P),也控制它)

引用中心
超出范围。将其置于
if
语句之外,并确保随后检查
null
,如下所示:

public String citiesNorthOf(String cityName){
String allCitiesNorthOf = "";// must be initialized

Point referenceCityCenter = null;

for(int i=0; this._cities[i] != null ; i++)
{
    if (this._cities[i].getCityName() == cityName)
    {
        referenceCityCenter = new Point(this._cities[i].getCityCenter());
    }
}

for(int i=0; this._cities[i] != null ; i++)//we don't need to exclude the comparable city itself because it will give a false
{
    if (referenceCityCenter !- null && this._cities[i].getCityCenter().isAbove(referenceCityCenter)) 
    {
        allCitiesNorthOf = allCitiesNorthOf.concat(this._cities[i].toString()+"\n\n");
    }
}
}

referenceCityCenter
超出范围。将其置于
if
语句之外,并确保随后检查
null
,如下所示:

public String citiesNorthOf(String cityName){
String allCitiesNorthOf = "";// must be initialized

Point referenceCityCenter = null;

for(int i=0; this._cities[i] != null ; i++)
{
    if (this._cities[i].getCityName() == cityName)
    {
        referenceCityCenter = new Point(this._cities[i].getCityCenter());
    }
}

for(int i=0; this._cities[i] != null ; i++)//we don't need to exclude the comparable city itself because it will give a false
{
    if (referenceCityCenter !- null && this._cities[i].getCityCenter().isAbove(referenceCityCenter)) 
    {
        allCitiesNorthOf = allCitiesNorthOf.concat(this._cities[i].toString()+"\n\n");
    }
}
}

referenceCityCenter
vs
referenceCityCenter
。请阅读变量范围。如果您试图为该变量赋值,还希望删除if语句中的“点”。我编辑了问题。。。现在有另一个问题了。。。请看。。谢谢!假设您已更正了导致原始错误的拼写错误,则需要更改名为
referenceCityCenter
的其中一个变量的名称。错误信息非常清楚:不能在一个方法中声明两个名称相同的变量。它怎么知道你说的是哪一个呢?现在你又改变了问题,请参考@SotiriosDelimanolis:“阅读变量范围。”
referenceCityCenter
vs
referenceCityCenter
。请阅读变量范围。如果您试图为该变量赋值,还希望删除if语句中的“点”。我编辑了问题。。。现在有另一个问题了。。。请看。。谢谢!假设您已更正了导致原始错误的拼写错误,则需要更改名为
referenceCityCenter
的其中一个变量的名称。错误信息非常清楚:不能在一个方法中声明两个名称相同的变量。它怎么知道你说的是哪一个呢?现在你又改变了问题,请参考@SotiriosDelimanolis:“阅读变量范围。”Yasmani修复了它!:)只要确保当他们要求您输入您在arrayYasmani中不知道的城市名称时,您控制空值(已修复!)只要确保当他们要求您输入数组中您不知道的城市名称时,您控制空值