#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    7
    Rep Power
    0

    Problem with java arithmetic.


    I get an error when trying to calculate the customer's monthly bill for Internet service provider1. my compiler (bluej) tells me it would result in loss of precision. thanks for the help guys!
    /**
    * Program: isp
    * Author: Myself
    * Date: September 14
    * Description, Calculates the customer's monthly bill.
    */
    import java.util.Scanner; //Needed for scanner class
    public class isp
    {
    public static void main(String []args)
    {
    int isp1; //Defines Internet Service Provider 1
    int isp2; //Defines Internet Service Provider 2
    int isp3; //Defines Internet Service Provider 3
    int number; //used for the decision between 1,2, and 3.
    int hours; //holds the number of hours used in the isp.
    int bill; //the monthly bill to be defined by any of the cases.
    Scanner keyboard = new Scanner(System.in); //adds the ability to use an external scanner

    System.out.println("Which # Internet Service Provider do you use? (1, 2, or 3):");
    number = keyboard.nextInt();
    switch (number)
    {
    Code:
                case 1:
                    System.out.println("You picked one");
                    System.out.println("How many hours of operation did you use?");
                    hours = keyboard.nextInt();
                    System.out.println("Hours used: " + hours);
                    isp1 = ((hours - 10)*2) + (9.95); //calculates the amount of money to use.
                    bill = isp1;
                    System.out.println("Your monthly bill (9.95$/10 hours, plus 2$/additional hour : " + bill);
                    break;


    case 2:
    System.out.println("You picked two");
    break;

    case 3:
    System.out.println("You picked three");
    break;

    default:
    System.out.println("Invalid option, please try again");
    break;
    }
    }
    }
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Can you show the statement where the error is?
    Also post the definitions for all variables used in that statement.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    7
    Rep Power
    0
    Originally Posted by NormR
    Can you show the statement where the error is?
    Also post the definitions for all variables used in that statement.
    Ok, I knocked off what is not important or the mean time:
    /**
    * Program: isp
    * Author: myself
    * Date: September 14
    * Description, Calculates the customer's monthly bill.
    */
    import java.util.Scanner; //Needed for scanner class
    public class isp
    {
    public static void main(String []args)
    {
    int isp1; //Defines Internet Service Provider 1.
    //isp1 is 9.95/month for 10 hours, plus 2$ extra hours
    int number; //used for the decision between 1,2, and 3.
    int hours; //holds the number of hours used in the isp.
    int bill; //the monthly bill to be defined by any of the cases.
    Scanner keyboard = new Scanner(System.in); //adds the ability to use an external scanner

    System.out.println("Which # Internet Service Provider do you use? (1, 2, or 3):");
    number = keyboard.nextInt();
    switch (number)
    {
    case 1:
    System.out.println("You picked one");
    System.out.println("How many hours of operation did you use?"); //asks how many additional hours used.
    hours = keyboard.nextInt();
    System.out.println("Hours used: " + hours);
    isp1 = ((hours - 10)*2) + 9.95; //calculates the amount of money to use. i get the error here, it says use a double for 9.95, and when I switch it to double, it says int required.
    bill = isp1;
    System.out.println("Your monthly bill (9.95$/10 hours, plus 2$/additional hour : " + bill);
    break;
    }
    }
    }
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Can you post the full text of the compiler's error message that shows the statement where the error occurs?

    Also just post the ONE statement where the error occurs, not the whole program.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    7
    Rep Power
    0
    Originally Posted by NormR
    Can you post the full text of the compiler's error message that shows the statement where the error occurs?

    Also just post the ONE statement where the error occurs, not the whole program.
    isp1 = ((hours - 10)*2) + 9.95; //calculates the amount of money to use. i get the error here, it says use a double for 9.95, and when I switch it to double, it says int required

    Compiler: possible loss of precision. required: int, found: double;
    if this is confusing, I am using bluej, and this is all I get
  10. #6
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    What value do you want back from the expression? An int (loses precision) or a double? The compiler is converting the right side to a double and trying to assign that to an int.
    You will need to do some casts to tell the compiler that you know what you are doing and intend to lose precision when assigning a double to an int.
    Last edited by NormR; September 16th, 2012 at 11:58 AM.

IMN logo majestic logo threadwatch logo seochat tools logo