Major Java Help

Discussion in 'Programming' started by demonlord07, Aug 14, 2013.

  1. #1
    Hey guys i'm coding this program and i have everything working except for the interest rate which brings out this error,

    MenuDrivenProgram.java:92: possible loss of precision
    found : double
    required: int
    deposit = balanceCurrent + interest;
    ^
    1 error

    Here is my code,

    public static void InvestmentReport()
        {
            System.out.printf("*** Investment Report stub ***\n");
            // This is where your Part A solution goes
            System.out.printf("*************** InvestmentReport Menu ***************\n\n");
            Scanner console=new Scanner(System.in);
            int deposit, monthly, interestRate;
            double interest, balanceCurrent;
            System.out.println ("Enter your initial deposit amount in dollars\n");
            deposit = console.nextInt();
             
            System.out.println ("Enter the annual interest rate as a percentage (eg. 6.0)\n");
            interest = console.nextDouble();
           
            System.out.println ("Enter your monthly deposit amount in dollars\n");
            monthly = console.nextInt();
           
            System.out.println ("Savings growth over the next 6 months:\n");
           
            System.out.println ("Balance after first month: $" + deposit);
            deposit = deposit + monthly;
            System.out.println ("Interest earned for this month: $" + interest);
            interest = balanceCurrent * interestRate / 12 / 100;
            deposit = balanceCurrent + interest;
           
            System.out.println ("Balance after second month: $" + deposit);
            deposit = deposit + monthly;
            System.out.println ("Interest earned for this month:\n");
           
            System.out.println ("Balance after third month: $" + deposit);
            deposit = deposit + monthly;
            System.out.println ("Interest earned for this month:\n");
           
            System.out.println ("Balance after fourth month: $" + deposit);
            deposit = deposit + monthly;
            System.out.println ("Interest earned for this month:\n");
           
            System.out.println ("Balance after fifth month: $" + deposit);
            deposit = deposit + monthly;
            System.out.println ("Interest earned for this month:\n");
           
            System.out.println ("Balance after sixth month: $" + deposit);
            deposit = deposit + monthly;
            System.out.println ("Interest earned for this month:\n");
        }
    Code (markup):
    Anyone able to tell me what i've done wrong and how to fix it? Cheers
     
    Solved! View solution.
    demonlord07, Aug 14, 2013 IP
  2. Designer6

    Designer6 Greenhorn

    Messages:
    26
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    13
    #2
    You are adding 2 double and then storing it in an int which is not allowed.
    explanation: deposite (int) = balanceCurrent (double) + interest (double);

    2 solutions: 1) you either change deposite to double.
    2) you cast, code: deposite= (int)(balanceCurrent + interest);

    Casting is forcing the type you want, if we take an example here with the code I provided
    lets assume that:
    balanceCurrent = 5.4 and interest=2.1

    deposit = 7

    If you don't understant contact me and I will help.
     
    Designer6, Aug 14, 2013 IP
  3. demonlord07

    demonlord07 Peon

    Messages:
    9
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    3
    #3
    Thanks i did get that working but i'm still having a problem. Here is my full code,

    import java.util.Scanner;
    
    public class MenuDrivenProgram
    {
        private static Scanner s = new Scanner(System.in);
       
        public static void main(String[] args)
        {
            String inputLine;
            char option = ' ';
           
            do
            {
                //Prints the line allowing you to select one of the valid options
                printMenu();
                System.out.printf("Please select a valid option: ");
                inputLine = s.nextLine();
               
                //Prints out an error message if length is equal to 1
                if (inputLine.length() != 1)
                {
                    System.out.printf("Error! You did not select a valid option!\n");
                    System.out.printf("Please try again\n\n");
                }
                else
                {
                    option = inputLine.charAt(0);
                    option = Character.toUpperCase(option);
                   
                    switch (option)
                    {
                            //Brings up the InvestmentReport menu
                        case 'A':
                            InvestmentReport();
                            break;
                           
                            //Brings up the RainfallReport menu
                        case 'B':
                            RainfallReport();
                            break;
                           
                            //Exits the program with a goodbye message
                        case 'X':
                            System.out.printf("Exiting the program - goodbye...\n\n");
                            break;
                           
                            //Errors out if keyed in value isn't a valid menu option
                        default:
                            System.out.printf("Error!" +
                                              "\"%c\" is not a valid menu option!\n\n",
                                              option);
                           
                    }
                }
               
               
            } while (option != 'X');
        }
        //This prints the main menu and let's you choose between 3 different options. InvestmentReport, RainfallReport and Exiting the program
        public static void printMenu()
        {
            System.out.printf("*************** Assignment 2 Menu ***************\n\n");
            System.out.printf("\tA. InvestmentReport.\n");
            System.out.printf("\tB. RainfallReport.\n");
            System.out.printf("\tX. Exit the program\n\n");
            System.out.printf("*************************************************\n\n");
        }
       
        public static void InvestmentReport()
        {
            System.out.printf("*** Investment Report stub ***\n");
            // This is where your Part A solution goes
            System.out.printf("*************** InvestmentReport Menu ***************\n\n");
            Scanner console=new Scanner(System.in);
            double deposit = 0D;
            double interest = 0D;
            double monthly = 0D;
            double interestRate = 0D;
            System.out.println ("Enter your initial deposit amount in dollars\n");
            deposit = console.nextDouble();
             
            System.out.println ("Enter the annual interest rate as a percentage (eg. 6.0)\n");
            interestRate = console.nextDouble();
           
            System.out.println ("Enter your monthly deposit amount in dollars\n");
            monthly = console.nextDouble();
           
            System.out.println ("Savings growth over the next 6 months:\n");
           
            deposit = deposit + monthly;
            interest = deposit * ((interestRate / 12) / 100);
            deposit = deposit + interest;
            System.out.println ("Balance after first month: $" + deposit);
            System.out.println ("Interest earned for this month: $" + interest);
            System.out.println ("\n");
           
            deposit = deposit + monthly;
            interest = deposit * interestRate / 12 / 100;
            deposit = deposit + interest;
            System.out.println ("Balance after second month: $" + deposit);
            System.out.println ("Interest earned for this month: $" + interest);
            System.out.println ("\n");
           
            deposit = deposit + monthly;
            interest = deposit * interestRate / 12 / 100;
            deposit = deposit + interest;
            System.out.println ("Balance after third month: $" + deposit);
            System.out.println ("Interest earned for this month: $" + interest);
            System.out.println ("\n");
           
            deposit = deposit + monthly;
            interest = deposit * interestRate / 12 / 100;
            deposit = deposit + interest;
            System.out.println ("Balance after fourth month: $" + deposit);
            System.out.println ("Interest earned for this month: $" + interest);
            System.out.println ("\n");
           
            deposit = deposit + monthly;
            interest = deposit * interestRate / 12 / 100;
            deposit = deposit + interest;
            System.out.println ("Balance after fifth month: $" + deposit);
            System.out.println ("Interest earned for this month: $" + interest);
            System.out.println ("\n");
           
            deposit = deposit + monthly;
            interest = deposit * interestRate / 12 / 100;
            deposit = deposit + interest;
            System.out.println ("Balance after sixth month: $" + deposit);
            System.out.println ("Interest earned for this month: $" + interest);
            System.out.println ("\n");
        }
       
       
        public static void RainfallReport()
        {
            System.out.printf("*** Rainfall Report stub ***\n");
            // This is where your Part B solution goes
            System.out.printf("*************** RainfallReport Menu ***************\n\n");
                Scanner sc = new Scanner(System.in);
                double[] fall = new double[7];
                for (int i = 0 ; i < 7 ; i++)
                {
                    System.out.print("Enter RainFall Measure In Millimetres: "  + (i+1) + ": ");
                    fall[i] = sc.nextDouble();
                }
                System.out.println("R.Fall Reverse Order ");
                for ( int i = 0;  i>=7    ;  i--)
                {
               
            }
        }
       
       
    }
    Code (markup):
    This is the data it should be displaying,

    Enter your initial deposit amount in dollars: 1000
    Enter the annual interest rate as a percentage (eg. 6.0): 6.0
    Enter your monthly deposit amount in dollars: 500
    Savings growth over the next 6 months:
    Balance after first month: $1507.50
    Interest earned for this month: $7.50
    Balance after second month: $2017.54
    Interest earned for this month: $10.04
    Balance after third month: $2530.13
    Interest earned for this month: $12.59
    Balance after fourth month: $3045.28
    Interest earned for this month: $15.15
    Balance after fifth month: $3563.00
    Interest earned for this month: $17.73
    Balance after sixth month: $4083.32
    Interest earned for this month: $20.32

    But instead it displays it like this,

    Enter your initial deposit amount in dollars

    1000
    Enter the annual interest rate as a percentage (eg. 6.0)

    6.0
    Enter your monthly deposit amount in dollars

    500
    Savings growth over the next 6 months:

    Balance after first month: $1507.5
    Interest earned for this month: $7.5
    Balance after second month: $2017.5375
    Interest earned for this month: $10.0375
    Balance after third month: $2530.1251875
    Interest earned for this month: $12.5876875
    Balance after fourth month: $3045.2758134375
    Interest earned for this month: $15.1506259375
    Balance after fifth month: $3563.002192504687
    Interest earned for this month: $17.726379067187498
    Balance after sixth month: $4083.3172034672107
    Interest earned for this month: $20.315010962523434

    As you can see it has too many decimal points and i need it to round off to the nearest number. How would i go about doing this? Please help? Thankyou.
     
    demonlord07, Aug 14, 2013 IP
  4. #4
    import java.text.DecimalFormat;
    import java.util.Scanner;

    public class MenuDrivenProgram
    {
    private static Scanner s = new Scanner(System.in);

    public static void main(String[] args)
    {
    String inputLine;
    char option = ' ';

    do
    {
    //Prints the line allowing you to select one of the valid options
    printMenu();
    System.out.printf("Please select a valid option: ");
    inputLine = s.nextLine();

    //Prints out an error message if length is equal to 1
    if (inputLine.length() != 1)
    {
    System.out.printf("Error! You did not select a valid option!\n");
    System.out.printf("Please try again\n\n");
    }
    else
    {
    option = inputLine.charAt(0);
    option = Character.toUpperCase(option);

    switch (option)
    {
    //Brings up the InvestmentReport menu
    case 'A':
    InvestmentReport();
    break;

    //Brings up the RainfallReport menu
    case 'B':
    RainfallReport();
    break;

    //Exits the program with a goodbye message
    case 'X':
    System.out.printf("Exiting the program - goodbye...\n\n");
    break;

    //Errors out if keyed in value isn't a valid menu option
    default:
    System.out.printf("Error!" +
    "\"%c\" is not a valid menu option!\n\n",
    option);

    }
    }


    } while (option != 'X');
    }
    //This prints the main menu and let's you choose between 3 different options. InvestmentReport, RainfallReport and Exiting the program
    public static void printMenu()
    {
    System.out.printf("*************** Assignment 2 Menu ***************\n\n");
    System.out.printf("\tA. InvestmentReport.\n");
    System.out.printf("\tB. RainfallReport.\n");
    System.out.printf("\tX. Exit the program\n\n");
    System.out.printf("*************************************************\n\n");
    }

    public static void InvestmentReport()
    {
    System.out.printf("*** Investment Report stub ***\n");
    // This is where your Part A solution goes
    System.out.printf("*************** InvestmentReport Menu ***************\n\n");
    Scanner console=new Scanner(System.in);
    double deposit = 0D;
    double interest = 0D;
    double monthly = 0D;
    double interestRate = 0D;

    DecimalFormat fmt = new DecimalFormat ("0.###");

    System.out.println ("Enter your initial deposit amount in dollars\n");
    deposit = console.nextDouble();

    System.out.println ("Enter the annual interest rate as a percentage (eg. 6.0)\n");
    interestRate = console.nextDouble();

    System.out.println ("Enter your monthly deposit amount in dollars\n");
    monthly = console.nextDouble();

    System.out.println ("Savings growth over the next 6 months:\n");

    deposit = deposit + monthly;
    interest = deposit * ((interestRate / 12) / 100);
    deposit = deposit + interest;
    System.out.println ("Balance after first month: $" + fmt.format(deposit));
    System.out.println ("Interest earned for this month: $" + fmt.format(interest));
    System.out.println ("\n");

    deposit = deposit + monthly;
    interest = deposit * interestRate / 12 / 100;
    deposit = deposit + interest;
    System.out.println ("Balance after second month: $" + fmt.format(deposit));
    System.out.println ("Interest earned for this month: $" + interest);
    System.out.println ("\n");

    deposit = deposit + monthly;
    interest = deposit * interestRate / 12 / 100;
    deposit = deposit + interest;
    System.out.println ("Balance after third month: $" + fmt.format(deposit));
    System.out.println ("Interest earned for this month: $" + fmt.format(interest));
    System.out.println ("\n");

    deposit = deposit + monthly;
    interest = deposit * interestRate / 12 / 100;
    deposit = deposit + interest;
    System.out.println ("Balance after fourth month: $" + fmt.format(deposit));
    System.out.println ("Interest earned for this month: $" + fmt.format(interest));
    System.out.println ("\n");

    deposit = deposit + monthly;
    interest = deposit * interestRate / 12 / 100;
    deposit = deposit + interest;
    System.out.println ("Balance after fifth month: $" + fmt.format(deposit));
    System.out.println ("Interest earned for this month: $" + fmt.format(interest));
    System.out.println ("\n");

    deposit = deposit + monthly;
    interest = deposit * interestRate / 12 / 100;
    deposit = deposit + interest;
    System.out.println ("Balance after sixth month: $" + fmt.format(deposit));
    System.out.println ("Interest earned for this month: $" + fmt.format(interest));
    System.out.println ("\n");
    }


    public static void RainfallReport()
    {
    System.out.printf("*** Rainfall Report stub ***\n");
    // This is where your Part B solution goes
    System.out.printf("*************** RainfallReport Menu ***************\n\n");
    Scanner sc = new Scanner(System.in);
    double[] fall = new double[7];
    for (int i = 0 ; i < 7 ; i++)
    {
    System.out.print("Enter RainFall Measure In Millimetres: " + (i+1) + ": ");
    fall = sc.nextDouble();
    }
    System.out.println("R.Fall Reverse Order ");
    for ( int i = 0; i>=7 ; i--)
    {

    }
    }


    }




    I am pretty sure that there is a way you format once, but I am too tired , to think. It is 4:00 am now and I am so sleepy :p This works and it is pretty simple, you add:
    DecimalFormat fmt = new DecimalFormat ("0.###");
    and whenever you want to display a formatted number you put: fmt.format(deposit)
     
    Designer6, Aug 14, 2013 IP
  5. Designer6

    Designer6 Greenhorn

    Messages:
    26
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    13
    #5
    sorry I don't know how to display a java file.
     
    Designer6, Aug 14, 2013 IP
  6. demonlord07

    demonlord07 Peon

    Messages:
    9
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    3
    #6
    Thanks so much for that it works perfectly.
     
    demonlord07, Aug 14, 2013 IP