Results 1 to 6 of 6
  1. #1
    Join Date
    Jun 2010
    Posts
    4
    Rep Power
    0

    Default How can I do this more efficiently ?

    Hi I'm hoping you can advise me on a piece of code I'm trying to write. This problem could be done very easily using an access database but I decided I wanted to do it in a standalone Java app, just for practice more than anything.

    I want the user to input a 3 later code which will return the corresponding City name and Country. I want to do this for about 100 Cities.

    e.g. user enter DUB program returns - "This Code is Dublin and the Country is Ireland"

    So far i've been able to do this using if else statements for a few datavalues but obviously I don't want to write if else statements for 100 values. Anyone have any advise or is it just stupid trying to do this with Java

    What I currently have

    String code;
    code = JOptionPane.showInputDialog(null, "Enter the 3 Digit Site Code?");

    if (code.equals("DUB")){

    JOptionPane.showMessageDialog(null, "This Site Code is Dublin, Counry is Ireland");}

    else
    if (code.equals("NYC")){

    JOptionPane.showMessageDialog(null, "This Site Code is Ney York Country is America");}

    else {
    JOptionPane.showMessageDialog(null, "Site Code Not Found");}

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Use a Map<String, City>
    The key would be "DUB" and the City class would encapsulate city and country as Strings.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    Join Date
    Jun 2010
    Posts
    4
    Rep Power
    0

    Default

    Hi Thanks for the quick reply. Not that familiar with Map but I'll do some research on it

    Cheers,

  4. #4
    Join Date
    Jun 2010
    Posts
    4
    Rep Power
    0

    Default

    Hi, I've been trying to figure this out. Would appreciate if someone could look at my below code and tell me if i'm on the right track. By the way i'm pretty much at beginner level. Thanks a lot.



    public class City {
    String sitename;
    String description;
    }



    City Dub = new City ("Dublin", "This is in Ireland");
    City NYT = new City ("New York", "This is in America");
    City LON = new City ("London", "This is in England");


    string userinput;
    userinput = JOptionPane.showInputDialog(null, "Enter the 3 Digit Code?");

    Map<userinput, City>

    JOptionPane.showMessageDialog(null, "This City is" + City.sitename + City.description;

    }

  5. #5
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    if you enter digits 001, 002 or 003 you will get a city and its description. here is the code

    Java Code:
    import java.util.HashMap;
    import java.util.Map;
    
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    
    public class City extends JFrame {
    	String sitename;
    	String description;
    
    	public City(String site, String desc) {
    		this.sitename = site;
    		this.description = desc;
    	}
    
    	public String toString() {
    		return sitename + ", " + description;
    	}
    
    	public static void main(String[] args) {
    
    		String userinput;
    		String digit;
    		City Dub = new City("Dublin", "This is in Ireland");
    		City NYT = new City("New York", "This is in America");
    		City LON = new City("London", "This is in England");
    
    		Map<String, City> m = new HashMap<String, City>();
    		m.put("001", Dub);
    		m.put("002", NYT);
    		m.put("003", LON);
    
    		userinput = JOptionPane
    				.showInputDialog(null, "Enter the 3 Digit Code?");
    
    		try {
    			JOptionPane.showMessageDialog(null, "This City is"
    					+ m.get(userinput).toString());
    		} catch (NullPointerException e) {
    			JOptionPane.showMessageDialog(null, "This City was not found!");
    		}
    	}
    }

  6. #6
    Join Date
    Jun 2010
    Posts
    4
    Rep Power
    0

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •