Hello Daniweb,

So, I am currently working on a simple download time estimator program using JOptionPane and some loops.
I have some beginning code and wanted to check how it is working, only it seems I messed up something and am stuck in my loop when I run it. I will keep working at what I am missing. Here is my code...

package downloadTimeApp;

/*
 * This program allows user input to find approximate download time.
 * by my name
 * date: 9/14/14
 */
import javax.swing.JOptionPane;

public class DownloadTimeApp {


     public static void main(String[] args)
        {
            // display operational messages

            String mBStr = JOptionPane.showInputDialog(null,
                    "Please enter file size (MB): ", 
                    "Welcome to the Download Time Estimator",
                    3);



            int mB = Integer.parseInt(mBStr);
            int mbSpeed = 0;
            int hours = 0;
            int min = 0;
            int sec = 0;


            // get a number from the user
            while (mB != 0)
            {              
                if (mB >= 1)
                {
                   mB = mB + 0;                    
                }
                else if (mB <= 0)
                    JOptionPane.showMessageDialog(null, "Invalid entry, not counted.", "Input Error", 2);

            }

            while (mbSpeed != 0)
            {
                if (mbSpeed >= 1)
                {
                    mbSpeed = mbSpeed + 0;
                }
                else if (mbSpeed <= 0)
                    JOptionPane.showMessageDialog(null, "Invalid entry, not counted.", "Input Error", 2);
            }



            while (hours != 0)
            {
                if (hours <= 0)
                {
                    hours = 0;
                    hours = 3600 / mB;
                }
                else if (hours >= 1)
                {
                    hours = hours + 0;
                }
            }

            // display the results  

            String message = "\n" +
                             "This download will take approximately" +
                             hours + "hours" + 
                             min + "minutes" + 
                             sec + "seconds";

            JOptionPane.showMessageDialog(null, message, "Show solution", 1);

        }

}

Ok I see what I did wrong with the loop, and have fixed that issue. But, now I am having an issue trying to display my result correctly. Ex. If I enter 800 for MB and 1 for download speed, I should get a result of 0 hours 13 minutes 20 seconds. But I am getting 0 hours 13 minutes and either 800 seconds or 0 seconds. Here is my updated code...

package downloadTimeApp;

/*
 * This program allows user input to find approximate download time.
 * by my name
 * date: 9/14/14
 */
import javax.swing.JOptionPane;

public class DownloadTimeApp {


     public static void main(String[] args)
        {
            // display operational messages

            String mBStr = JOptionPane.showInputDialog(null,
                    "Please enter file size (MB): ", 
                    "Welcome to the Download Time Estimator",
                    3);



            int mB = Integer.parseInt(mBStr);
            int totalSec = 0;
            int hours = 0;
            int min = 0;
            int sec = 0;


            // get a number from the user
            while (mB <= 0)
            {              
                if (mB >= 1)
                {
                   mB = mB + 0;                    
                }
                else if (mB <= 0)
                    JOptionPane.showMessageDialog(null, "Invalid entry, not counted.", "Input Error", 2);

                mBStr = JOptionPane.showInputDialog(null,
                        "Please enter file size (MB): ", 
                        "Welcome to the Download Time Estimator",
                        3);
                mB = Integer.parseInt(mBStr);

            }

            String mbSpeedStr = JOptionPane.showInputDialog(null,
                    "Please enter download speed (MB/sec): ", 
                    "Welcome to the Download Time Estimator",
                    3);

            int mbSpeed = Integer.parseInt(mbSpeedStr);

            while (mbSpeed <= 0)
            {
                if (mbSpeed >= 1)
                {
                    mbSpeed = mbSpeed + 0;
                }
                else if (mbSpeed <= 0)
                    JOptionPane.showMessageDialog(null, "Invalid entry, not counted.", "Input Error", 2);

                mbSpeedStr = JOptionPane.showInputDialog(null,
                        "Please enter download speed (MB/sec): ", 
                        "Welcome to the Download Time Estimator",
                        3);

                mbSpeed = Integer.parseInt(mbSpeedStr);
            }



                totalSec = mB / mbSpeed;
                hours = totalSec / 3600;
                min = totalSec / 60;




            // display the results  

            String message = "\n" +
                             "This download will take approximately " +
                             hours + " hours " + 
                             min + " minutes " + 
                             sec + " seconds";

            JOptionPane.showMessageDialog(null, message, "Show solution", 1);

        }

}

I think you need to subtract hours from main sum, then subtract minutes from the main sum and you will have secounds you need.
For examples you have 8000 secounds when you divede that with 60 to get minutes so you have 133.3 so now you need to divide that with 60 to get hours. Sou you get 2 hours.
You now calculate it like this 8000 - 7200 (2h represented in secounds) = so you have rest of 800 secounds. Now again divide it by 60 to get minutes you get 13.3 minutes now you use 13 minutes * 60 secounds you have 780 secounds sou you allready have 2 hours, 13 minutes and secounds you get by subrtacting 800 - 780 and you have 20 secounds...
You need to implement something like that. I wanted first to make you try to do that, if you again have trouble with code i will help you with coding...
Mike.

Ok I think I see what your saying. I'll play with that a bit and of I run into issues I'll let you know. Thanks Mike

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.