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 7 of 7

Thread: Issue display result in time format

  1. #1
    Junior Member
    Join Date
    Oct 2020
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Issue display result in time format

    hello,

    I'm trying to display a result from 2 inputs calculations from minutes to HHMM, however when I try and calculate it with the below code only the HH changes to the current hour. eg. 129 will only display as 02:00. I have tested it by adding num1 with num2 and this does indeed display as hhmm, but I need the multiplications & divisions to take place

    I'm new to this so any advice is appreciated.

      EditText e1 = (EditText) findViewById(R.id.Distance);
        EditText e2 = (EditText) findViewById(R.id.Height);
        TextView t1 = (TextView) findViewById(R.id.Result);
        Integer num1 = Integer.parseInt(e1.getText().toString());
        Integer num2 = Integer.parseInt(e2.getText().toString());
        Integer sum = (int) ((num1 / 5 * 60) + (num2 / 600 * 60));
     
        int hours = sum / 60;
        int minutes = sum % 60;
     
            t1.setText(String.format("Estimated time: %d hr : %02d min", hours, minutes));
        }
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Issue display result in time format

    Can you print out and post some examples of input to the code and the output that the code creates?
    What values are in num1 and num2?
    What Strings are returned by the getText methods of e1 and e2?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Oct 2020
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Issue display result in time format

    The inputs:
    e1 represents journey distance in km , so for example user enters "5"
    e2 represent height of summit in m, so user could enter "900"

    num1 is the value of e1 divided by 5 then multiplied by 60
    num2 is the value of e2 divided by 600 then multiplied by 60

    Both results then need added together to give a total in minutes.

    I'm unable to attach an image of the output, result. I hope that was what you asked for.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Issue display result in time format

    What should be the values of hour and minutes for those values of "5" and "900"?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Oct 2020
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Issue display result in time format

    If the user entered 5 & 900 the final value would be = 150 (2hours & 30min)

    Currently the output only displays 02hr:00min

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Issue display result in time format

    The code is using integer math: 4/10 = 0 Change it to use decimal: 4/10.0 = 0.4
    If you don't understand my answer, don't ignore it, ask a question.

  7. The Following User Says Thank You to Norm For This Useful Post:

    java_d (October 21st, 2020)

  8. #7
    Junior Member
    Join Date
    Oct 2020
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Issue display result in time format

    Amazing!! So simple, thanks!

Similar Threads

  1. Regarding reading of DATE FORMAT ISSUE and its VALIDATION PROBLEM
    By Naresh0523 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 6th, 2017, 01:44 AM
  2. Yet another level format issue...
    By Gravity Games in forum Android Development
    Replies: 14
    Last Post: August 20th, 2012, 12:53 PM
  3. Issue with reverse engineering what format this guy expects. Out of ideas
    By Udustar in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 22nd, 2011, 06:40 AM
  4. Issue with output format
    By mael331 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 25th, 2011, 02:12 PM
  5. Beginner trying to write Java code, has issue w/ printing result and 2 decimals
    By flpanthers1 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 5th, 2011, 11:11 AM