Results 1 to 5 of 5
  1. #1
    busdude is offline Member
    Join Date
    Oct 2008
    Posts
    25
    Rep Power
    0

    Default Simple program involving military time

    write a program that reads two times in military format like (0900, 1730) and prints the number of hours and minutes between the two times.

    output should be like
    '
    please enter the first time: (user enters 0900)

    please enter second time: (user enters 1730

    8 hours 30 minutes
    '

    how do i do this?

  2. #2
    ojn
    ojn is offline Senior Member
    Join Date
    Aug 2008
    Location
    Stockholm, Sweden
    Posts
    119
    Rep Power
    0

    Default

    No one's gonna write the entire thing for you. That won't help you at all. Post the code you've come up with so far and ask us specific questions about where you're stuck.

  3. #3
    busdude is offline Member
    Join Date
    Oct 2008
    Posts
    25
    Rep Power
    0

    Default

    import java.util.Scanner;

    public class Main
    {



    public static void main(String[] args)
    {
    Scanner scanner = new Scanner(System.in) ;
    TimeInterval timeInterval = new TimeInterval(time1, time2);

    System.out.println("Enter two times in military format, using 4 digits.");

    int time1 = scanner.nextInt() ;
    int time2 = scanner.nextInt() ;






    System.out.println("The number of hours is.:");
    System.out.println(timeInterval.getHours(time1, time2));
    System.out.println("The number of minutes is.:");
    System.out.println(timeInterval.getMinutes(time1, time2));

    }
    }








    public class TimeInterval
    {

    int time1 = scanner.nextInt() ;
    int time2 = scanner.nextInt() ;
    private int hr;
    private int min;




    hr = ((time2 - time1)/100)-(((time2 - time1)/100) % 100);




    public int getHours(){

    return hr;
    }

    public int getMinutes(){

    return min;
    }
    }

  4. #4
    ojn
    ojn is offline Senior Member
    Join Date
    Aug 2008
    Location
    Stockholm, Sweden
    Posts
    119
    Rep Power
    0

    Default

    There are so many errors with that code, I don't know where to begin. You should really go back to the very basics and re-read the chapters about variable declarations, passing arguments to constructors and methods and the Scanner class. As of now, your code is a complete mess.

    Also: When you paste code, use the CODE tags.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,578
    Rep Power
    25

    Default

    If you are getting errors, please copy and paste them here. We can see lots of errors, but the compiler will see all of them. It saves us lots of time if you'll post the errors.

    Add some println to show intermediate values.
    Break long expressions up into sub parts and display the results of each part. If the following does NOT work, break it up into single steps and display the results of each.
    hr = ((time2 - time1)/100)-(((time2 - time1)/100) % 100);

Similar Threads

  1. Simple Addition Program Outputting Wrong Value
    By carlodelmundo in forum New To Java
    Replies: 4
    Last Post: 08-05-2008, 03:37 AM
  2. Not able to debug simple hello world program
    By amit123solanki in forum New To Java
    Replies: 1
    Last Post: 06-12-2008, 03:41 PM
  3. Peculiarty in code of simple program...
    By Kreuz14 in forum New To Java
    Replies: 4
    Last Post: 01-23-2008, 03:27 AM
  4. help with simple program in java
    By katie in forum New To Java
    Replies: 2
    Last Post: 08-06-2007, 08:03 PM
  5. help with simple java program
    By leonard in forum New To Java
    Replies: 3
    Last Post: 07-30-2007, 09:40 AM

Posting Permissions

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