我的循环布尔是不是编译 [英] My loop boolean is not compiling

查看:218
本文介绍了我的循环布尔是不是编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要这个方法经过我的ArrayList myZip,寻找是否有在那里,匹配INT拉链整数,然后去寻找邮编code等于intZip;如果没有匹配,则返回null。

I need this method to go through my ArrayList myZip, find whether or not there is an integer in there that matches int zip, then go and find the ZipCode that is equal to intZip; if there is no match then return null.

public ZipCode findZip(int zip) {
    for (int i = 0; i < myZips.size(); i++) {
        if (zip == myZips.get(i)) 
            return myZips.get(i);}
        return null;
}

任何想法?

这里的邮编code类:

Here's the ZipCode Class:

public class ZipCode {

    private int zipCode;
    private String city;
    private String state;
    private double longitude;
    private double latitude;


    public ZipCode(int pZip) {
        zipCode   = pZip;
        city      = "UNKOWN";
        state     = "ST";
        latitude  = 0.0;
        longitude = 0.0;
    }

    public ZipCode (int pZip, String pCity, String pState, double pLat, double pLon) {
        zipCode   = pZip;
        city      = pCity;
        state     = pState;
        latitude  = pLat;
        longitude = pLon;

    }

    public void setZipCode(int zipCode){
        this.zipCode = zipCode;
    }

    public void setCity (String city){
        this.city = city;
    }

    public void setState (String state){
        this.state = state;
    }

    public void setLatitude (double latitude){
        this.latitude = latitude;
    }

    public void setLongitude (double longitude){
        this.longitude = longitude;
    }

    public int getZipCode (){
        return zipCode;
    }

    public String getCity (){
        return city;
    }

    public String getState(){
        return state;
    }

    public double getLatitude(){
        return latitude;
    }

    public double getLongitude(){
        return longitude;
    }

    public String toString(){
        return city + ", " + state + zipCode;
}
}

我真的只需要知道如何使findZip方法返回邮编code,它是同为int拉链。

I really just need to know how to make the findZip method return the ZipCode that is the same as int zip.

这最终会从具有格式如下地址的file.txt的拉动:
94594,梅里维尔,WA,经度,纬度

This is eventually going to pull from a file.txt that has addresses formatted like this: 94594, MerryVille, WA, Longitude, Latitude

推荐答案

我猜你想这样的事情。

List<ZipCode> myZips = // create all ZipCode's somehow

public ZipCode findZip(int zip){

    //look at all zips
    for(ZipCode zipCode : myZips){

        //if we find one that matches the one we want, return it
        if(zipCode.getZipCode() == zip){
            return zipCode;
        }
    }

    // we checked all our zip's and couldn't find this one
    return null;
}

这篇关于我的循环布尔是不是编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆