-1

Here is the code:

public class CarLoan {
    public static void main(String[] args) {
        int carloan = 10000;
        int loanlengh = 3;
        int interestrate = 5;
        int downpayment= 2000;
        if (loanlengh <= 0 || interestrate <= 0);   
        System.out.printl("no");
        else if (downpayment > carloan);
        System.out.printl("thats not how it works");

        else
            int remainingbalance = carloan -downpayment;
        int months = loanlengh * 12;
        int monthlybalance = remainingbalance / months;
        int interest = placeholder /100;
        int placeholder = (monthlybalance * interestrate);
        int monthlypayment = monthlybalance + interest;
        System.out.println(monthlypayment);
    }
}

Here are the errors:

CarLoan.java:13: error: variable declaration not allowed here int remainingbalance = carloan -downpayment;

I don't get the issue.

fixed errors:

CarLoan.java:9: error: 'else' without 'if' else if (downpayment > carloan);

jhamon
  • 3,603
  • 4
  • 26
  • 37
  • 2
    `if (loanlengh <= 0 || interestrate <= 0); ` and `else if (downpayment > carloan);` need to remove `;` – flyingfox Aug 03 '18 at 08:46
  • 3
    Please do not use `if`, `else` and `else if` without **curly braces** `{` `}`. Especially as beginner you easily run into bugs like that. Without braces only the next line belongs to the `if`. So for your last `else` only the following line is inside, the rest is outside of the `else` again. Also, please correct your indents. – Zabuzard Aug 03 '18 at 08:48

5 Answers5

1

The problem is in your if statement line, where you are doing :

if (loanlengh <= 0 || interestrate <= 0);

Note the semicolon ; at the end. Due to that, the if statement ends there. As the Error mentions :

error: 'else' without 'if'

Hence the error.

Kaushik NP
  • 6,733
  • 9
  • 31
  • 60
1
if (loanlengh <= 0 || interestrate <= 0) // removed `;`
    System.out.printl("no");
else if (downpayment > carloan);
    System.out.printl("thats not how it works");

You have added a semicolon at the end of if statement.

Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212
  • 1
    Please add in text what was wrong in the code from the original poster and what you have done to solve the problem. Answers that consist only of code are not very useful. – Sumurai8 Aug 03 '18 at 10:28
1

Need to remove ; for below two lines

if (loanlengh <= 0 || interestrate <= 0);   

else if (downpayment > carloan);
flyingfox
  • 13,414
  • 3
  • 24
  • 39
1

Remove ; after if an else if statements. And add curly braces { } after else to include all necessary lines in else-section.

if (loanlengh <= 0 || interestrate <= 0) // removed `;`
    System.out.printl("no");
else if (downpayment > carloan) // removed `;`
    System.out.printl("thats not how it works");
else { 
    ... // added curly braces `{ }`
}

You should better format your code to avoid such mistakes.

And try to always use curly braces { } with if-else, it helps.

if (loanlengh <= 0 || interestrate <= 0) {
    System.out.printl("no");
} else if (downpayment > carloan) {
    System.out.printl("thats not how it works");
} else {
    ...
}
Daria Pydorenko
  • 1,754
  • 2
  • 18
  • 45
1

your problem is at line 7, 9 and else statement. remove ; at line 7 and line 9

make this correction at else (Line 12 here), it will solve it. i have tested.

 else
       {int remainingbalance = carloan -downpayment;}

Use if else control flow properly

if (condition){
 //statements here
}
else if (condition){
 //statements here
}
else {
//statements here
}
The Scientific Method
  • 2,374
  • 2
  • 14
  • 25