I need to check and see if the city in the text field is acity listed in the map
ID: 3614508 • Letter: I
Question
I need to check and see if the city in the text field is acity listed in the map.....I tried astraight comparison below and well it does not work anyideas on how to check to see if it is listed???by the way the action listener is NOT in the same class at theCity array(9 classes away)
private City[] vertices = {new City("Seattle", 75, 50),
new City("San Francisco", 50, 210),
new City("Los Angeles", 75, 275), newCity("Denver", 275, 175),
new City("Kansas City", 400, 245),
new City("Chicago", 450, 100), new City("Boston",700, 80),
new City("New York", 675, 120), newCity("Atlanta", 575, 295),
new City("Miami", 600, 400), new City("Dallas",408, 325),
new City("Houston", 450, 360) };
jbtBFS.addActionListener(new ActionListener() { public voidactionPerformed(ActionEvent e) { String city =(jtfCity.getText()); if (city!="Seattle" || city!="SanFrancisco" || city!="Los Angeles" || city!="Denver" ||city!="Kansas City" || city!="Chicago" ||city!="Boston" || city!= "New York" || city!="Atlanta" ||city!="Miami" || city!="Dallas" || city!="Houston") { JOptionPane.showMessageDialog(null, city + " is notin the map!!"); } else {
} } });
} I need to check and see if the city in the text field is acity listed in the map.....I tried astraight comparison below and well it does not work anyideas on how to check to see if it is listed???
by the way the action listener is NOT in the same class at theCity array(9 classes away)
private City[] vertices = {new City("Seattle", 75, 50),
new City("San Francisco", 50, 210),
new City("Los Angeles", 75, 275), newCity("Denver", 275, 175),
new City("Kansas City", 400, 245),
new City("Chicago", 450, 100), new City("Boston",700, 80),
new City("New York", 675, 120), newCity("Atlanta", 575, 295),
new City("Miami", 600, 400), new City("Dallas",408, 325),
new City("Houston", 450, 360) }; private City[] vertices = {new City("Seattle", 75, 50),
new City("San Francisco", 50, 210),
new City("Los Angeles", 75, 275), newCity("Denver", 275, 175),
new City("Kansas City", 400, 245),
new City("Chicago", 450, 100), new City("Boston",700, 80),
new City("New York", 675, 120), newCity("Atlanta", 575, 295),
new City("Miami", 600, 400), new City("Dallas",408, 325),
new City("Houston", 450, 360) };
jbtBFS.addActionListener(new ActionListener() { public voidactionPerformed(ActionEvent e) { String city =(jtfCity.getText()); if (city!="Seattle" || city!="SanFrancisco" || city!="Los Angeles" || city!="Denver" ||city!="Kansas City" || city!="Chicago" ||city!="Boston" || city!= "New York" || city!="Atlanta" ||city!="Miami" || city!="Dallas" || city!="Houston") { JOptionPane.showMessageDialog(null, city + " is notin the map!!"); } else {
} } });
}
Explanation / Answer
I assume that there is some accessor for thename of the city, such as getName(). Then you could check the citynames as follows: publicvoid actionPerformed(ActionEvent e) { String city= jtfCity.getText(); boolean found= false; for(City v: vertices) { if(v.getName().equals(city)) { found = true; break; } } if(!found) { JOptionPane.showMessageDialog(null, city +" is not in the map!!"); } else {} } That solution is both more elegant and more general; yours involveshardcoding the names of cities. Furthermore, your comparison is incorrect - you cannot compareStrings using ==. Formore information, look here: /-answers-5-654794-cpi0.aspx
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.