Results 1 to 4 of 4

Thread: Help me please!

  1. #1
    MLIAKIRA is offline Member
    Join Date
    Oct 2013
    Posts
    14
    Rep Power
    0

    Default Help me please!

    Okay so here is my code. For my project we have to calculate the monthly payment for a mortgage. My professor wants us to set it up so when someone enters a negative number for any of the inputs it still lets you enter the rest of them but at the end, the program outputs a message that says "ALL NUMERICAL NUMBERS MUST BE POSITIVE!". I understand the if-then-else statements but I just don't know how to make the program JUST send out the message and not do the calculations if the number is negative. Also, how would I make it so "The monthly payment is: " not even show up, this is what I was trying to do in the beginning, and just put out the error message just once at the very end where it show the monthly payment, that's what my professors looking for.Someone please help!
    (Also, with the if statement the guy on the other java forums told me to do this and it doesn't work at all, I know)
    Java Code:
         
            Scanner in = new Scanner(System.in);
            
            
            double loanAmount;
            double interestRate;
            double numberYears;
            double months;
            double monthlyPayment;
            
                    
                    
            System.out.print("Enter the loan amount: ");
            loanAmount = Double.parseDouble(in.nextLine());
    
            System.out.print("Enter the rate: ");
            interestRate = Double.parseDouble(in.nextLine());
            
            System.out.print("Enter the number of years: ");
            numberYears = Double.parseDouble(in.nextLine());
            
            months = numberYears * 12;
            
          if (loanAmount < 0 || interestRate < 0 || numberYears < 0)
    {
    	System.out.println("ALL NUMERICAL VALUES MUST BE POSITIVE!");
            
    }
    else 
    {
    	interestRate = interestRate / 100 / 12;
            
            monthlyPayment = interestRate * loanAmount * (Math.pow(1+interestRate, months))/(Math.pow(1+interestRate, months)-1);
    
            NumberFormat defaultFormat = NumberFormat.getCurrencyInstance();
            System.out.println("The monthly payment is: " +defaultFormat.format(monthlyPayment));
        System.out.println(monthlyPayment = interestRate * loanAmount * (Math.pow(1+interestRate, months))/(Math.pow(1+interestRate, months)-1));
    }
    This is my goal for what it should look like...
    Help me please!-wefdsfgsedfg.png
    Last edited by MLIAKIRA; 10-09-2013 at 10:26 PM.

  2. #2
    MLIAKIRA is offline Member
    Join Date
    Oct 2013
    Posts
    14
    Rep Power
    0

    Default Re: Help me please!

    please?

  3. #3
    MLIAKIRA is offline Member
    Join Date
    Oct 2013
    Posts
    14
    Rep Power
    0

    Default Re: Help me please!

    no one....?

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •