Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Military time converter

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Military time converter

    hello I am new to the forum and to java and i have a program that the user inputs the time followed by am and pm and the program changes it to military time. at the end the program asks if you want to rerun the program. the program works when i enter no but i cant figure out how to get it to work if the user enters yes. also if the time is am the military time is the same as the input except there is a 0 in front. i cant figure out how to output the 0. +0 wont work i have added my code below!

    import java.util.*;
     
    public class Time{
     
     public static void main(String [ ] args) {
    Scanner input=new Scanner(System.in);
    int hour, minute;
    int hour24,change;
    String ampm,answer;
    boolean yes=true;
    boolean no=false;
    System.out.println("Enter the time followed by am or pm, and I will convert:");
    hour= input.nextInt();
    minute=input.nextInt();
    ampm=input.next();
     
    while (yes){
    hour24 = Time.convertHour(hour, ampm);
    print_time(hour24, minute);
    System.out.println("The time is: " +hour24 + ":" +minute);
    System.out.println("Would you like to run again? Yes Or No");
    answer=input.next();
    if (answer.equals("yes"))
    {
    System.out.println("Please enter time followed by am or pm:");
    yes=true;
    }
     
    else if(answer.equals("no"))
    	yes=false; //break;
    }
    }
     
     
    public static int convertHour(int hours_12, String am_pm){
     if(hours_12==12 && am_pm.equals("am"))
     hours_12=( hours_12-12);
     else if(hours_12<=11 && am_pm.equals("am"))
     hours_12=(hours_12);
     else if (hours_12<=11 && am_pm.equals("pm"))
     hours_12=hours_12+12;
     return hours_12; // <--------- I added this return statement
     
    }
     public static void print_time(int hours_24, int mintues) {
     System.out.println(hours_24+ ":" +mintues) ;
    }
    }
    Last edited by Json; May 12th, 2011 at 03:51 AM. Reason: Java highlight tags added


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Military time converter

    Hello, your problem with the zero is the fact that you store the hour in an int, the int cannot start with a zero, there would be no point in that so you need to read in the hour and after your conversion to 24h format look at the digit and if it's less than 10 you need to add a zero before it and remember to store it as a string to keep the initial zero.

    Hope that makes sense.

Similar Threads

  1. J2ME converter program
    By sackling in forum What's Wrong With My Code?
    Replies: 0
    Last Post: December 6th, 2010, 09:13 AM
  2. converter program, problem with action listener
    By robertson.basil in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 2nd, 2010, 06:44 AM
  3. phonetic to cyrillic converter
    By Brt93yoda in forum Java Theory & Questions
    Replies: 1
    Last Post: September 5th, 2010, 03:26 PM
  4. Using time
    By ellias2007 in forum Java Theory & Questions
    Replies: 1
    Last Post: September 4th, 2010, 09:48 AM
  5. Piglatin Converter
    By jross21 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 14th, 2010, 01:09 PM

Tags for this Thread