• Post Reply Bookmark Topic Watch Topic
  • New Topic

I keep getting Error code and can't figure it out just trying to make simple download calculator.  RSS feed

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.until.Scanner;
import java.text.NumberFormat;
import java.lang.Math;
import java.math.*;
import java.math.RoundingMode;
import javax.swing.JOptionPane;// Impots JOptionPane class.

public class DownloadAppJRP
{
public static void main (String[] args)
{
// Welcome user to program
System.out.println("Welcome to the Download Time Estimator");

// create Scanner object and start a while loop
Scanner sc = new Scanner(System.in);
        String choice = "y";
        while (choice.equalsIgnoreCase("y"))

//get interactive user input
String enter = JOptionPane.showInputDialog(
"Enter a File Size (MB): ");

//get interactive user input
String downloadSpeed = JOptionPane.showInputDialog(
"Enter Download Speed (MB/Sec): ");
// calculate the hour, minutes and seconds
String totalSeconds;
String totalMinutes;
String totalHours;
int hour;
int minutes;
int seconds;
totalSeconds = Size / downloadSpeed;
totalSeconds = (double) ((int) (totalSeconds+.5));
totalHours = ((int) (totalSeconds))/3600;
totalSeconds%=3600;
totalMinutes = ((int) (totalSeconds))/60;
totalMinutes%=60;


NumberFormat number = NumberFormat.getNumberInstance();
number.setMaximumFractionDigits(0);


String message =
        "This download will take approximately " + number.format(totalHours) +
        " hours\n"   + number.format(totalMinutes) + " minutes\n"
  + number.format(totalSeconds) + " seconds\n"  ;
System.out.println(message);



        // see if the user wants to continue
        System.out.print("Continue? (y/n): ");
        choice = sc.next();
        System.out.println();

}
}
 
Saloon Keeper
Posts: 3330
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please cut and paste the entire error message into a new post.

Please repost your code and enclose the listing inside Code tags. Highlight the code, the click on the "Code" button just above the edit window. You can see if it worked correctly by clicking the "Preview" button below the edit window.
 
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your posted code won't even compile.  Please post only working code.
 
Carey Brown
Saloon Keeper
Posts: 3330
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You've defined totalSeconds (and others) of being of type String and then you're trying to perform math operations on them. You need a numeric type instead.
 
Ranch Hand
Posts: 134
4
Eclipse IDE Hibernate Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joshua Perrine wrote:
// create Scanner object and start a while loop
Scanner sc = new Scanner(System.in);
        String choice = "y";
        while (choice.equalsIgnoreCase("y"))


You're creating a Scanner presumably to read user input from the keyboard but later you use the JOptionPane to allow users to input values instead.  This seems inconsistent.  It would be better to read in the File Size and Download Speed values via the console and convert them to numberical values (use a try-catch statement to ensure you can handle any Number Format Exceptions that might arise if a user unexpectedly enters alphabetic characters when they should instead enter numeric ones.)
 
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, I avoid using a Scanner, because I have a utility class for keyboard input; it hides a Scanner but it can ensure taht you get an int when you want an int. By using a loop, you can avoid number format exceptions and input mismatch exceptions completely.It has lots of other methods. I can challenge you to overload that method so it only returns sNa value in a particular range. Rob Spoor taught me how to write such loops.

That way you can use that class for input and you don't need anything else. You can create more methods for other sorts of input. Just beware of nextLine() after nextSomethingElse().

SR is right that you shou‍ld avoid mixing Scanner input and option pane input. Option pane input is  very old‑fashioned programming style.

Stop using the old number format classes: use a formatter or System.out.printf, and the % tags instead.
 
Campbell Ritchie
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and welcome to the Ranch
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!