Results 1 to 8 of 8
  1. #1
    greystreet34 is offline Member
    Join Date
    Oct 2012
    Posts
    8
    Rep Power
    0

    Default Converting Military Time

    Hello, I'm working on an assignment where I'm supposed to convert military time (hhmm) to 12-hour time. The format is supposed to be:

    1935 becomes 7:35 pm
    935 becomes 9:35 am
    1900 becomes 7 pm
    0035 becomes 12:35 am

    I can't seem to fix some mistakes I keep getting, which seem to be when 0's are introduced, and I'm not sure where they're happening. For example:

    935 = 9:35 am (good)
    1335 = 1:35 pm (good)
    1900 = 7:0 pm (bad, expected 7pm)
    0035 = '' (not returning anything. should say 12:35 am)
    909 = 9:9 am (should say 9:09 am)

    Anyways, here's my code and thanks for any advice.

    Java Code:
    import java.util.Scanner;
    
    public class TimeConvert 
    {
        public static String militaryToOrdinaryTime(int milTime)
        {
           int hour = milTime / 100;
           int min = milTime - (milTime/100*100);
           String period;
           
           if (hour < 1 || hour > 24 || min < 0 || min > 59)
           {
               return "";
           }
           if (hour > 12)
           {
               hour = hour - 12;
               period = "pm";
           }
           else
           {
               period = "am";
           }
           String ordTime = hour + ":" + min + " " + period;
           return ordTime;
        }
        
        public static void main(String[] args) 
        {
            Scanner in = new Scanner(System.in);
            System.out.print("Enter military Time (hhmm) : ");
            int time = in.nextInt();
            
            System.out.println("Ordinary Time: " +militaryToOrdinaryTime(time));
        }
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Converting Military Time

    First, so that you can see what's going on, print out some more diagnostic information.

    Java Code:
    public static String militaryToOrdinaryTime(int milTime)
    {
       int hour = milTime / 100;
       int min = milTime - (milTime/100*100); // or min=milTime%100
       String period; // why is this here?
    
       System.out.print("milTime=" + milTime);
       System.out.print("  hour=" + hour);
       System.out.println("  min=" + min);
            
       if (hour < 1 || hour > 24 || min < 0 || min > 59)
       {
           return "";
       }
       if (hour > 12)
       {
           hour = hour - 12;
           period = "pm";
       }
       else
       {
           period = "am";
       }
       String ordTime = hour + ":" + min + " " + period;
       return ordTime;
    }
    0035 = '' (not returning anything. should say 12:35 am)
    What is hour in this case? Does your first if statement make sense given that value?

    1900 = 7:0 pm (bad, expected 7pm)
    What is min in that case? Does the expression ordTime=hour+":"+min +" "+ period; make sense for that value - or indeed any value of min less than 10?

  3. #3
    greystreet34 is offline Member
    Join Date
    Oct 2012
    Posts
    8
    Rep Power
    0

    Default Re: Converting Military Time

    Thanks for the reply! I did some work and fixed most of them except I'm not sure how to give min a leading 0 if 0 < min < 10. Should I convert min to a String and make min = "0" + min? Although when I do that Netbeans says "int cannot be dereferenced."

    Also, I would print out more diagnostic information but I'm still new and not sure how yet.

    Java Code:
    import java.util.Scanner;
    
    public class TimeConvert 
    {
        public static String militaryToOrdinaryTime(int milTime)
        {
           int hour = milTime / 100;
           int min = milTime % 100;
           String period;
           
           if (hour < 0 || hour > 24 || min < 0 || min > 59)
           {
               return "";
           }
           else if (hour > 12)
           {
               hour = hour - 12;
               period = "pm";
           }
           else
           {
               period = "am";
           }
           if (hour == 0)
           {
               hour = 12;
           }
           else if (min == 0)
           {
               String ordTime = hour + " " + period;
               return ordTime;
           }   
           else if (min < 10 && min > 0)
           {
               // needs fixing
           }
           String ordTime = hour + ":" + min + " " + period;
           return ordTime;
        }
        
        public static void main(String[] args) 
        {
            Scanner in = new Scanner(System.in);
            System.out.print("Enter military Time (hhmm) : ");
            int time = in.nextInt();
            
            System.out.println("Ordinary Time: " +militaryToOrdinaryTime(time));
        }
    }

  4. #4
    greystreet34 is offline Member
    Join Date
    Oct 2012
    Posts
    8
    Rep Power
    0

    Default Re: Converting Military Time

    Ok, I got it to work using this:
    Java Code:
               String min1 = String.valueOf(min);
               min1 = "0" + min1;
               String ordTime = hour + ":" + min1 + " " + period;
               return ordTime;
    Although we never learned the valueOf class I assume it's ok to use. Not sure what else the teacher would have us do. Thanks for the help :)

  5. #5
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Converting Military Time

    Cross posted at Converting Military Time

  6. #6
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Converting Military Time

    "valueOf" is a method, not a class. There is a big difference!

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Converting Military Time

    Well done! Sorry I didn't get back before now.

    I think the errors you got when you started using a string might have arisen because you tried to use the string for the arithmetic and comparisons. You can't do that. Using two variables as you do now is, conceptually, the most straight forward.

    Since it all depends on whether the number is less than 10 I would use an if statement and say something like

    Java Code:
    String ordTime;
    if(min < 10) {
        ordTime = hour + ":0" + min + " " + period;
    } else {
        ordTime = hour + ":" + min + " " + period;
    }
    return ordTime;
    I see someone posted some advice about cross posting at the other site. It really would be a good idea to post a note there saying that you've solved the problem, and how.

  8. #8
    greystreet34 is offline Member
    Join Date
    Oct 2012
    Posts
    8
    Rep Power
    0

    Default Re: Converting Military Time

    Thanks for the advice, and information about cross posting. I didn't know which forums I should post in so if I continue to post in 2 different places I'll be sure to link the topics from now on.

Similar Threads

  1. Replies: 2
    Last Post: 11-10-2012, 10:27 AM
  2. converting string (GMT date) to date in US time.
    By JRuyechan in forum New To Java
    Replies: 1
    Last Post: 10-15-2010, 07:07 AM
  3. Military Time
    By ustar in forum New To Java
    Replies: 1
    Last Post: 03-02-2010, 04:01 AM
  4. Write a Program that reads two times in military format
    By HPcompaq256 in forum New To Java
    Replies: 10
    Last Post: 02-26-2010, 04:38 AM
  5. Simple program involving military time
    By busdude in forum New To Java
    Replies: 4
    Last Post: 10-08-2008, 06:03 PM

Posting Permissions

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