简体   繁体   中英

How to display JOptionPane through user input

I'm currently stuck on trying to figure out why my Array values aren't diplaying in the dialog box. I am doing an assignment whose details are:

"Write a program named TallestBuildingLookupthat contains an array of 10 TallestBuildingobjects and fill in it with the data given above accordingly. Then use dialog boxes to accept a building name and display the building's location, height, and stories. If a match is not found, display an error message that includes the invalid name and allow the user to search for a new building name."

My main issue is getting my array values to display from my toString() method, and handling the exception when I don't receive a name in the array. Specifically, getting the dialog box to loop to re-enter a name value and recheck the array. Any help would be greatly appreciated.

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.");
            }
        }
    }
}

Try this:

TallestBuilding tallestBuilding = null;
for (int i=0; i<10; i++){
    if(name.equals(tallestbuilding[i].getName())){
       tallestBuilding = tallestbuilding[i];
       break;
    }
}
if(tallestBuilding == null) {
    JOptionPane.showInputDialog("Sorry - no "+ name + " was found.");
} else {
    JOptionPane.showMessageDialog(null, tallestBuilding);
}
  1. Dont use '==' to compare Strings, because it compares a reference, not the value it self. Check Java String.equals versus ==
  2. Use a MessageDialog to show the user a value, not a InputDialog
  3. You should break the for loop after displaying the user
  4. You should save the object found in a temp reference to display it after.

Specifically, getting the dialog box to loop to re-enter a name value and recheck the array.

Well, you need to ask for input within a loop of some type, something that you are not doing in your code. There are two main flavors of loop -- a for loop which is used when you know in advance how many times you wish to loop (here you don't), -- and a while loop or do-while loop when you don't know in advance. I suggest that you use a do-while loop since you want to get input from the user at least once, meaning the loop has to run at least once if not more, and then keep looping until the input is valid. I'd use a boolean, say called boolean inputValid = false; for this.

Other issue: Your for loop is broken. You react to match vs no-match within the loop, which is not correct, since if you do this, you're going to give the user an error dialog for every mis-match, which is not what you want. Instead you want to check for matches within the loop, set a boolean if a match is found, perhaps using the one I mentioned above, and then after the loop is complete, display the error message if no match is found, and then repeat the do-while loop as I mentioned.

My main issue is getting my array values to display from my toString() method, and handling the exception when I don't receive a name in the array.

If you need specific help on this issue, you're going to have to tell us more about what's wrong with your displayed data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM