Home Super Mario Bros Games Puzzle Games Physics Games Flash Games Two Player Games Games Walkthroughs
Java Program Tutorials

Only Best Physics Games

I'm fairly new to programming in general and need some help. I am using the Java.util.TimeZone to retrieve the IDs(city names) and there time zone. I am using a hashmap to implement this. I have put the city names and the time zones in the map and I am now trying to ask the user to enter a city they wish to get the time zone of. However, in my loop I have a validation check to make sure the city name is in the hashmap..not only is it not working but the loop also does not break. It correctly puts out the time it is currently but not the correct timezone for the city(I have typed various city names and all have about the same timezone). After printing out the local time it is in the city the user can choose to end the program by "saying yes". If the user enters yes then the loop should break and the the program should end. If they enter anything else it should continue. Could someone please help me fix this! Here is my code.

 import java.util.*;
  import java.util.TimeZone;
  import java.util.Date;
  import java.text.DateFormat;
  import java.util.HashMap; 



public class Maps {
public static void main(String[] args)
{    

String[] Zone = TimeZone.getAvailableIDs();
int i = 0; 
for (i =0 ; i< Zone.length; i++)
{

 String zone1 = Zone[i].replaceAll("_"," "); 

 if(zone1.indexOf('/') != -1)
{   
  zone1 = zone1.substring(zone1.indexOf('/')+1);
}
 TimeZone tz = TimeZone.getTimeZone(zone1);

 HashMap hm = new HashMap();
 HashMap <String, Integer> map = new HashMap <String, Integer>();
hm.put(zone1, tz);

 //System.out.println(hm);

 while(hm != null)
 {
java.util.Scanner input = new java.util.Scanner(System.in);
    System.out.println("City?");
    String city = input.nextLine(); 
    boolean CityExist = hm.containsKey(city);
    if (CityExist == true)
    { System.out.println("I can not find a match to the city, sorry. ");
    break;
    }

  TimeZone tz2 =   TimeZone.getTimeZone(city);
    DateFormat timeFormatter = DateFormat.getTimeInstance(); 
 Date now = new Date();
    System.out.println("Here: " + timeFormatter.format(now));
          System.out.print("Local Time: ");
          timeFormatter.setTimeZone(tz2); 
          System.out.println(timeFormatter.format(now));
     System.out.println("If you would like to quit please enter yes: ");
     String user = input.nextLine();


     if (user.equals ("yes") || user.equals("Yes"))
     {
         break; 
     }


 }  

    }

View the Original Page Here: Using hashmaps, breaking loops and user input in java     Back to the List


Only Best Physics Games


Java Program Examples
RSS4Java.com is dedicated to popular Java technology posts, blogs and entries.
Copyright© 2010 Rss4Java.com. Java is a trademark of Sun Microsystems, Inc. All blogs, posts and entries are the property of their authors.