# Please check my Java program

• 09-29-2011, 11:09 PM
Zora
Hey guys.

I'm currently working on a Java assignment and I was wondering if there are any problems in my code.

Here's the question:
Quote:

Write an algorithm and a program to settle the following question. A bank account starts out with an initial balance, iBalance. Interest is computed monthly at 6% per year (0.5% per month). Every month \$500.00 is withdrawn to meet education expenses. After how many years is the account depleted?
There are some values for which the algorithm you developed may not terminate. Modify your algorithm to make sure it always terminates. The program should read the iBalance from command line.

The input of the program is iBalance

Examples of the output of the program:
6 years and 1 month

1 year and 6 months

8 months (note that the output should not be as 0 years and 8 month)

Note: Withdraws will be at the end of each month. For example if the deposit is equal to 500\$, then at the end of the first month the iBalance will be equal to 500*(1+0.06/12) = 502.50 \$.

By taking 500\$, the iBalance will be equal to 2.5\$, and at the end of the second month the iBalance will be equal to 2.51 \$. As the result at the end of the second month the account will be depleted.
And here's my code:
Code:

```public class lab3 {   public static void main (String[] args)   {     float aNumber=0;     // Read input argument     if (args.length < 1){         System.out.println("Input a number");         System.exit(0); // Terminate the program if user has not input a number       }       else         aNumber = Float.parseFloat(args[0]);             month = 0       year = 0       int iBalance; //Input user's balance       iBalance * 0.005;       If (iBalance > 500) {       iBalance * 0.005 - 500;       month += month       }       Else       System.out.println("It will take " + month + "month(s) for the account to deplete.")       If (month > 12) {       year += year       }       If (year > 100) {       System.out.println("It will take " + years + "year(s) and " + month + "month(s) for the account to deplete.");   } }```
I don't want corrections for my code, just explanations as to why it is wrong so that I can figure out a solution.

By the way, I'm very new to Java and also on this forum, so I don't know how things work around here, but I appreciate your time and effort to help me out. :D
• 09-29-2011, 11:19 PM
Iron Lion
Re: Please check my Java program
Lots of errors there, as you're probably aware. Best thing to do is try to compile it and see what errors it generates, and use that as a starting point to fix it.

In particular, note that you have to terminate statements with a semicolon, not a new line. Also, don't capitalise keywords like 'if' and 'else'; the compiler won't recognise them as keywords if you do.
• 09-29-2011, 11:36 PM
Zora
Re: Please check my Java program
Yeah I'm aware of that. Syntax errors are fine, I just get confused on where to put semicolons and curly braces. I'm more focused on the logical errors, like does it make sense?
• 09-29-2011, 11:39 PM
Iron Lion
Re: Please check my Java program
Makes sense to me, but that doesn't matter; you need it to make sense to the compiler. Fix the syntax errors and then you can start worrying about whether it's doing what you expect it to.
• 09-29-2011, 11:42 PM
Zora
Re: Please check my Java program
I will do that. Alright thanks for your guidance.
• 09-29-2011, 11:42 PM
Fubarable
Re: Please check my Java program
There are so many errors that it suggests that you're not compiling your code frequently enough. If you're not using an IDE like Eclipse or NetBeans, you should compile your code very frequently, probably after every one to two new lines of code, and then don't add any new code until all compilation errors have been fixed.
• 09-29-2011, 11:43 PM
Fubarable
Re: Please check my Java program
Quote:

Originally Posted by Zora
Yeah I'm aware of that. Syntax errors are fine, I just get confused on where to put semicolons and curly braces. I'm more focused on the logical errors, like does it make sense?

No it does not. You have it backwards as you cannot test logic if syntax is off.