現在、配列値がダイアログボックスで表示されない理由を把握しようとしています。ユーザー入力からJOptionPaneを表示する方法
"TallestBuildingLookupthatという名前のプログラムには、10個のTallestBuildingオブジェクトの配列が含まれており、それに応じて上記のデータを入力します。次に、ダイアログボックスを使用して建物名を受け入れ、建物の位置を表示します一致するものが見つからない場合は、無効な名前を含むエラーメッセージを表示し、ユーザーが新しい建物名を検索できるようにします。
私の主な問題は、私の配列の値を私のtoString()メソッドから表示して、配列に名前を受け取っていないときに例外を処理することです。具体的には、ダイアログボックスをループさせて名前の値を再入力し、配列を再チェックします。どんな助けでも大歓迎です。
import javax.swing.*;
public class TallestBuildingLookup {
static class TallestBuilding{
private String name;
private String city;
private int height;
private int stories;
public TallestBuilding(String name, String city, int height, int stories) {
this.name = name;
this.city = city;
this.height = height;
this.stories = stories;
}
public String getName(){
return this.name;
}
public String toString(){
return this.name + " of " + this.city + ", "+ this.stories + "stories/" + this.height + " feet high." ;
}
}
public static void main(String[] args){
TallestBuilding[] tallestbuilding = new TallestBuilding[10];
tallestbuilding[0] = new TallestBuilding("One World Trade Center", "New York", 1776, 104);
tallestbuilding[1] = new TallestBuilding("Willis Tower", "Chicago", 1451, 108);
tallestbuilding[2] = new TallestBuilding("Empire State", "New York", 1250, 102);
tallestbuilding[3] = new TallestBuilding("Bank of America Tower", "New York", 1200, 55);
tallestbuilding[4] = new TallestBuilding("Aon Center", "Chicago", 1136, 83);
tallestbuilding[5] = new TallestBuilding("John Hancock Center", "Chicago", 1127, 100);
tallestbuilding[6] = new TallestBuilding("Wells Fargo Plaza", "Houston", 992,71);
tallestbuilding[7] = new TallestBuilding("Comcast Center", "Philidelphia", 974, 57);
tallestbuilding[8] = new TallestBuilding("Columbia Center", "Seattle", 967, 76);
tallestbuilding[9] = new TallestBuilding("Key Tower", "Clevland", 947, 57);
String entry = JOptionPane.showInputDialog("Enter a builing name");
String name = (String) entry;
System.out.println(name);
for (int i=0; i<10; i++){
if(name == tallestbuilding[i].getName()){
JOptionPane.showInputDialog(null, tallestbuilding[i]);
}
else{
JOptionPane.showInputDialog("Sorry - no "+ name + " was found.");
}
}
}
}
私の答えをチェックし、あなたに合っているかどうかを確認してください! – BrunoDM