I have been asked to count the overal time of an album.
The user enters minutes and seconds in the formmat of mm.ss.
I have wrote the folowing code but I don't get the result I supposed to get.
I.E : 1st:3.55, 2nd 3.55 I got 7.1 instead of 7.50.
Any help would be appreciated.

public class TestClass {


    static String delimeter = "\\.";
    static int sec;
    static int min;

    public static void main (String args[])
    {

            Scanner in = new Scanner(System.in);

            System.out.println("Enter track time in mm.ss");
            double first = in.nextDouble();

            System.out.println("Enter track time in mm.ss");
            double second = in.nextDouble();

            double overall = first +second;

            String trackLength = String.valueOf(overall);


            String[] songLengthSplit = trackLength.split(delimeter);

            min = Integer.valueOf(songLengthSplit[0]);
            sec = Integer.valueOf(songLengthSplit[1]);

            int minToSeconds = min * 60;
            int overallMinutes = (minToSeconds + sec) /60;
            int reminder = (minToSeconds + sec) % 60;



            System.out.print("Overall time is: " + overallMinutes + "minutes " + reminder + "seconds");

Recommended Answers

All 3 Replies

You try to add two mm.ss as if they were ordinary decimal numbers. Eg 1.40 + 1.40 is 3.20, but added as decimals you get 2.80 which is wrong.
Easiest solution is probably to covert the inputs to number of seconds, add those, then convert back to mins & secs

I thought that I convert tin the following two lines James

int minToSeconds = min * 60;
int overallMinutes = (minToSeconds + sec) /60;
int reminder = (minToSeconds + sec) % 60;

James I suppose that I convert min to sec by

`min * 60
then do I have to convert sec to sec?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.