从类中获取变量

时间:2017-03-31 20:20:46

标签: java

所以我有一个名为Buildings的类,它创建了7个构建对象并将它们添加到构建类型的ArrayList中:

package model;

import java.util.ArrayList;

/**
 *
 * @author dominic
 */
public class Buildings {
    private ArrayList<Building> buildingList = new ArrayList<Building>();


    public ArrayList<Building> getBuildingList() {
        return buildingList;
    }

    public void setBuildingList(ArrayList<Building> buildingList) {
        this.buildingList = buildingList;
    }

    public Buildings(){
        Building building1 = new Building("Kingston University","Penrhyn Road","KT1 2EE");
        buildingList.add(building1);
          Building building2 = new Building("Guild of Students","Penrhyn Road","KT1 2EE");
        buildingList.add(building2);
          Building building3 = new Building("Kingston University Health Centre","Penrhyn Road","KT1 2EE");
        buildingList.add(building3);
          Building building4 = new Building("Knights Park Centre","Grange Road","KT1 2QJ");
        buildingList.add(building4);
          Building building5 = new Building("Roehampton Vale","Friars Avenue","SW15 5DW");
        buildingList.add(building5);
          Building building6 = new Building("Kingston Law School","Kingston Hill","KT2 7LB");
        buildingList.add(building6);
          Building building7 = new Building("Kingston Business School","Kingston Hill","KT2 7LB");
        buildingList.add(building7);
    }


}

建筑类内部看起来像这样:

package model;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author dominic
 */
public class Building {
    private String buildingName;
    private String streetName;
    private String postcode;

    public Building(String buildingName, String streetName, String postcode){
    this.buildingName = buildingName;
    this.streetName = streetName;
    this.postcode = postcode;
    }
    public String getBuildingName() {
        return buildingName;
    }

    public void setBuildingName(String buildingName) {
        this.buildingName = buildingName;
    }

    public String getStreetName() {
        return streetName;
    }

    public void setStreetName(String streetName) {
        this.streetName = streetName;
    }

    public String getPostcode() {
        return postcode;
    }

    public void setPostcode(String postcode) {
        this.postcode = postcode;
    }

}

我在项目的其他地方(在servlet中)有一个for循环,它创建对象建筑物并循环遍历每个对象并将它们与用户输入进行比较。我已经测试过以确保该程序实际上是用户输入的。但是,字符串streetName和postCode仍为空。谢谢。

  HttpSession session = request.getSession();
    Integer id = (Integer)session.getAttribute("id");
     Buildings buildings = new Buildings();
       String streetName = "";
   String postCode = "";
   String postCodeInput = request.getParameter("postCodeInput");
    for(int i = 0; i > 6; i++)
    {


        if (postCodeInput == buildings.getBuildingList().get(i).getPostcode())
        {
            streetName = buildings.getBuildingList().get(i).getStreetName();
            postCode = buildings.getBuildingList().get(i).getPostcode();

        }


    }

          request.setAttribute("streetName", streetName);
    request.setAttribute("postCode", postCode);      
          request.setAttribute("postCodeInput", postCodeInput);
    request.getRequestDispatcher("StreetNameView.jsp").forward(request, response);

1 个答案:

答案 0 :(得分:1)

for(int i = 0; i > 6; i++)

那应该是<

for(int i = 0; i < 6; i++)

更好的是,直接迭代列表项,而不是索引。

for (Building building: buildings.getBuildingList())
{
    if (postCodeInput == building.getPostcode())
    {
        streetName = building.getStreetName();
        postCode   = building.getPostcode();

        break;
    }
}

您甚至可以使用Java 8流进行匹配。

Building building = buildings.getBuildingList().stream()
    .filter(b -> b.getPostcode() == postCodeInput)
    .findAny()
    .orElseThrow(NoSuchElementException::new);

request.setAttribute("streetName", building.getStreetName());
request.setAttribute("postCode",   building.getPostcode());