Mortgage Calculator

will u please put the whole Javascript of the mortgage calculator here
I know its in the video but still
My reason:- when I put “/” at the ending it says its the illegal start of expression

sry if this is silly

What do you mean “/”? explain me

import java.text.NumberFormat;
import java.util.Scanner;
public class main
{
public static void main(String[]args)
{
final byte MONTHS_IN_YEAR = 12;
final byte PERCENT = 100;

Scanner scanner = new Scanner(System.in);

System.out.println("Principal: ");
    int principal = scanner.nextInt();

System.out.println("Annual Interest Rate");
     float annualInterest = scanner.nextFloat();
      float monthlyInterest = annualInterest / PERCENT / MONTHS_IN_YEAR;

System.out.println("Period (Years): ");
     byte years = scanner.nextByte();
     int numberOfPayments = years * MONTHS_IN_YEAR;

double mortgage = principal*(monthlyInterest * Math.pow(1 + monthlyInterest, numberOfPayments))/(Math.pow(1 + monthlyInterest, numberOfPayments) - 1);

String mortgageFormatted = NumberFormat.getCurrencyInstance();
System.out.print("Mortgage: " + mortgageFormatted);






 }
 
 
 }

will u please let it run it would mean a lot to me

thx bro for helping but i got the program run

Hey! :smile: your code does not run because you need to add in the mortgageFormatted line the mortgage, like this:

String mortgageFormatted = NumberFormat.getCurrencyInstance().format(mortgage);
        System.out.println("Mortgage: "+mortgageFormatted);

Try it and let me know if it works for you.

This is the code, try it and let me know if it works for you :grin:

public static void main(String[] args) {
        final byte MONTHS_IN_YEAR = 12;
        final byte PERCENT = 100;

        Scanner scanner = new Scanner(System.in);

        System.out.print("Principal: ");
        int principal = scanner.nextInt();

        System.out.print("Annual Interest Rate: ");
        float annualInterest = scanner.nextFloat();
        float monthlyInterest = annualInterest / PERCENT / MONTHS_IN_YEAR;

        System.out.print("Period (Years): ");
        byte years = scanner.nextByte();
        int numberOfPayments = years * MONTHS_IN_YEAR;

        double mortgage = principal *
                (monthlyInterest * Math.pow(1+monthlyInterest,numberOfPayments))
                /(Math.pow(1+monthlyInterest,numberOfPayments)-1);

        String mortgageFormatted = NumberFormat.getCurrencyInstance().format(mortgage);
        System.out.println("Mortgage: "+mortgageFormatted);
    }
1 Like

Thanks i needed this ( .format(mortgage):wink: :slight_smile:
You don’t see it in the video and i don’t find the source code from Mosh.
Greetz

No need for calculating, your loan 1$ and return 2$ :slight_smile:

Can someone post the actual code that works. In the video Mosh said the code is attached below the video but is not there. My code runs well but is different, so I just want to see the recommended answer.

I mean programs are not exactly perfect systems where there is a right answer for “how to do it correctly” so it would probably be better to post your solution (in a new thread) and ask for feedback. I did it my own way when I was solving these exercises because I have already been writing Java code for years. I am sure it would not match exactly the code that Mosh would come up with.

My code works, just wanted to compare mine with the ones in the videos and could not find the full suggested solution. I managed to figure out the missing pieces. In the video Mosh says the solution is attached but I did not see it.

I think it is in the “Source Code” download from way earlier in the course up in the “Getting Started” section here. Inside the “Section 4 - Clean Coding/Finish/Mortgage Calculator” directory.

1 Like

I totally get your frustration with the JavaScript code. Unfortunately, I can’t provide the entire code here, but I suggest checking out real estate agency websites as they usually have mortgage calculators built-in.

I’m want to learn about Java programming

If you’re getting an “illegal start of expression” error, it’s often related to a missing semicolon or a syntax issue in your code. Double-check your code for any of those sneaky culprits.
Also, make sure you’re using the correct slashes (“/” or “”) in your code, as mixing them up can lead to unexpected errors.

It’s great that you’re diving into JavaScript and building a mortgage calculator. However, posting the entire JavaScript code here might be a bit too much. Instead, I’d recommend checking the video thoroughly for any syntax errors or misplaced characters. The error you’re getting might be a simple typo.

And by the way, if you ever need some financial advice related to mortgages, you could look into a Mortgage Broker in Rotherham. They might have some useful insights to complement your coding journey.

Nice you are in Java Part 1!!! I finished that and it was fun. The second part is pretty hard, just remember to never quit like I did in the second part and study some topics if you don’t understand

If you’re following a video tutorial, it might be helpful to compare your code step by step with the one in the video to spot any differences.
Don’t hesitate to seek assistance on coding forums or communities; there are usually friendly experts willing to lend a hand.
Lastly, if you ever find yourself needing advice or guidance on financial matters, you can consider checking out Mortgage Advice Harrogate. They offer useful resources to help you out.