Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Beginner at Java, Need help with my project its due tuesday.

  1. #1
    Join Date
    Oct 2013
    Thanked 0 Times in 0 Posts

    Default Beginner at Java, Need help with my project its due tuesday.


    This assignment is comprised of modifying your program which will calculate a monthly a moritzed amount (e.g. a house mortgage). For this program, you are to add the logic to prompt the user if they wish to enter the data again, then loop if they enter a "y" value; otherwise perform the calculations and the error checking as in the previous program.

    Enter loan amount: 100000
    Enter rate: 6
    Enter number years: 30

    The monthly payment is: $599.55

    Would you like to calculate again (y/n): y

    Enter loan amount: -100000
    Enter rate: 6
    Enter number years: 30

    You must enter positive numeric data!

    Would you like to calculate again (y/n): n

    __________________________________________________ __________________

    package monthypayment2;
    import java.text.NumberFormat;
    import java.util.Scanner;
    public class Monthypayment2 {
        private static double monthlypay1;
        private static String choice;
        private static boolean n;
        private static String False;
        private static boolean y;
         * @param args the command line arguments
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            //format number and currency
           NumberFormat NF = NumberFormat.getCurrencyInstance();
           NumberFormat NI = NumberFormat.getNumberInstance();
            double months = 1;
            double numyears;
            double loanamount;
            double rate;
           int repeat = 0;   //condition for loop
           do {
            //prompt loan amount
            System.out.print("Enter Loan Amount:");                           
            loanamount = in.nextDouble();
            //prompt rate
            System.out.print("Enter Rate:");
             rate = in.nextDouble();
             //prompt years
            System.out.print("Enter Number of Years:");
            numyears = in.nextDouble();
          //calculation monthly
           monthlypay1 = (loanamount * rate / 12) / (Math.pow(1 + rate, months)) / (Math.pow(1 + rate, months)-1);
            System.out.println("The Monthly Payment is: $" + monthlypay1);
             y = true;
             n = false;
            if (monthlypay1 < 0) {
                System.out.print("You need to enter positive numerical data!");}
            else {
                System.out.print("Would you like to continue calculations(y/n)?");
                choice = in.nextLine();
           } while (choice.equals("y"));
                    while(choice.equals("n")) {
                break; }

    Basically when I run the program the Math is still off despite me getting help and doing all suggestions that users gave me on here. I compared it with the formula and it's correct.
    When i put in a negative loan amount i get this error
    "Exception in thread "main" java.lang.NullPointerException"
    at line 65(which would be
     while (choice.equals("y"));
    after asking if I would like to calculate again.
    I've restarted a couple times on the project but I keep running into the same problems.
    Can anyone help me?

    https://scontent-b-iad.xx.fbcdn.net/...95&oe=526636AF <--- formula that I am using.

  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    My Mood
    Thanked 167 Times in 158 Posts

    Default Re: Beginner at Java, Need help with my project its due tuesday.

    private static String choice;
    The variable choice is not initialised so it is given the default value of null. If your code never assigns a value to choice then you get the NPE. You should always initialise your variables with appropriate values.
    Improving the world one idiot at a time!

  3. #3
    Join Date
    Oct 2013
    Thanked 0 Times in 0 Posts

    Default Re: Beginner at Java, Need help with my project its due tuesday.

    for the math I tried breaking it down but still doesn't calculate correctly.
    double calc1 = loan * rate;
            double calc2 = Math.pow(1 + rate, months);
            double calc3 = Math.pow(1 + rate, months)-1;
            double calc4 = calc1 / calc2;
            monthlypay1 = calc4 / calc3;

Similar Threads

  1. Project Nursing Home Due Tonight Help
    By morrism35 in forum What's Wrong With My Code?
    Replies: 17
    Last Post: October 16th, 2013, 07:17 PM
  2. Final Project Help Please? Due Tomorrow.
    By tylerreece22 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: April 23rd, 2012, 08:32 AM
  3. Have a C++ project due soon and am not sure if I'm using pointers right.
    By javapenguin in forum Other Programming Languages
    Replies: 12
    Last Post: September 19th, 2011, 09:54 PM
  4. Help please - urgent (Project due and not done) - Easy answer
    By Bagzli in forum Java Theory & Questions
    Replies: 1
    Last Post: March 2nd, 2011, 05:36 PM

Tags for this Thread