2017-03-31 13 views
0

から変数を持つガイド:は、だから私は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; 
    } 

} 

私はオブジェクトビルドを作成し、各オブジェクトをループしてユーザー入力と比較するプロジェクト(サーブレット内)のどこかに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
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()); 
+0

私の愚かな間違いを発見していただきありがとうございます。問題は、変数 "$ {streetName}"を参照すると、何も出力されない、Javaサーブレットからjspにこれをポストしようとするときです。 – Aueisbiejei3i939