# Whats Wrong? Beginning Code!

• September 20th, 2012, 06:49 PM
LoganC
Whats Wrong? Beginning Code!
Here is my code I have to debug for my computer programming class. I don't know what else to do! I have fixed a few, but probably 3 or 4 left. Thanks!

/*
Chapter 3 Debugging Assignment
Programmer:
Date:
Program Name: Bert,java
Purpose:
*/

import java.util.*;

public class bert
{
public static void main(String[] args)
{

//Declaring Variables
double annualInterest, payment;
String custName;

Scanner reader = new Scanner (System.in);

//Get Input from User
System.out.println("What is your first name? ");
System.out.print("What is the price of the car? ");
System.out.print("What is the downpayment? ");
System.out.print("What is the trade-in value? ");
System.out.print("For how many months is the loan? ");
System.out.print("What is the decimal interest rate? ");

//Output
}

public static void calculatePayment(int price, double downPayment, int tradeIn,
int months, double annualInterest)
{
int interest;
int loanAmt;
double payment;
int custName;
int downpayment;

//Calculations
interest = annualInterest / 12;
payment=loanAmt/((1/interest)-(1/(interest*Math.pow(1+interest months))));

System.out.print("The monthly payment for " + custName + " is \$");
System.out.println(payment);

return;
}
}
• September 20th, 2012, 10:25 PM
ChicoTheMan94
Re: Whats Wrong? Beginning Code!
It would make your code look a lot better if you wrapped it with [code=java] and [ /code] (with no space.)

Looking over your code, You're calling the math function, yet you don't have it imported, it may have to do with your compiler automatically loading it, but I'd add it in there, just in case. You are also going to get a loss of precision error with your code:
Code java:

`interest = annualInterest/12;`
because you could end up working with decimals, however interest is an int variable. Also in you're calculations part
Code java:

`payment=loanAmt/((1/interest)-(1/(interest*Math.pow(1+interest months))));`
You seem to have unnecessary spaces in there, unless it's something that happened in the transfer to this post. That's all I can find for now, it's really late. If you run it and you run into even more problems you can't seem to figure out, just post the error the compiler is giving you back here and I'm sure we can help out!
• September 21st, 2012, 07:09 AM
LoganC
Re: Whats Wrong? Beginning Code!
The only problem I am getting now that I haven't fixed is at the bottom.

System.out.print("The monthly payment for " + custName + " is \$");
System.out.println(payment);

return;

custName is in red and says cannot find symbol,
and return is saying it is an unnecessary return statement.
Based on my code above, why is custName saying it cannot find its symbol? It has been declared as you can see above.
• September 21st, 2012, 07:14 AM
Norm
Re: Whats Wrong? Beginning Code!
There are two variables named: custName. One a String and one an int. It does not make sense to have a name that is int.
Is the posted code the same code that you are getting the "cannot find symbol" error from?

[code=java]
[/code]
to get highlighting and preserve formatting.
• September 21st, 2012, 08:10 AM
LoganC
Re: Whats Wrong? Beginning Code!
Thanks guys fixed that, now im down to this. All errors are gone but just trying to run now, and im getting the errors below.
Im getting close!

Code java:

```/*** Chapter 3 Debugging Assignment Programmer: Date: Program Name: Bert,java Purpose: ***/   import java.util.Scanner;   public class bert { public static void main(String[] args) {   //Declaring Variables int price, downPayment, tradeIn, months; double annualInterest; String custName;   Scanner reader = new Scanner (System.in);   //Get Input from User System.out.println("What is your first name? "); custName = reader.next(); System.out.print("What is the price of the car? "); price = reader.nextInt(); System.out.print("What is the downpayment? "); downPayment = reader.nextInt(); System.out.print("What is the trade-in value? "); tradeIn = reader.nextInt(); System.out.print("For how many months is the loan? "); months = reader.nextInt(); System.out.print("What is the decimal interest rate? "); annualInterest = reader.nextDouble();       //Output calculatePayment (price, downPayment, tradeIn, annualInterest, months, custName);   }   public static void calculatePayment(int price, int downPayment, int tradeIn, double annualInterest, int months, String custName) { int interest; double loanAmt; double payment;             //Calculations interest = (int)annualInterest / 12; loanAmt = price-downPayment-tradeIn; payment = loanAmt/((1/interest)-(1/(interest*Math.pow(1+interest,months))));     System.out.print("The monthly payment for " + custName + " is \$"); System.out.println(payment);     } }```

Exception in thread "main" java.lang.ArithmeticException: / by zero
at bert.calculatePayment(bert.java:59)
at bert.main(bert.java:40)
Java Result: 1
Line 59 = payment = loanAmt/((1/interest)-(1/(interest*Math.pow(1+interest,months))));
Line 40-46 =
calculatePayment (price, downPayment, tradeIn, annualInterest, months, custName);

I hope I can figure out whats wrong!
• September 21st, 2012, 08:13 AM
Norm
Re: Whats Wrong? Beginning Code!
Quote:

/ by zero
Look at the code on line 59 and make sure the divisor is not 0.
Break the code up into simple statements that do one thing at a time and find out why the results are 0.

Print out the values of all the variables used on line 59 .
• September 21st, 2012, 08:17 AM
LoganC
Re: Whats Wrong? Beginning Code!
Hey Norm, I just added my whole code in the post above.
• September 21st, 2012, 09:00 AM
Norm
Re: Whats Wrong? Beginning Code!
Did you find what variable was zero?
If not, add a println() statement to print out the values of all the variables used in the divisor to see.
• September 21st, 2012, 12:24 PM
LoganC
Re: Whats Wrong? Beginning Code!
I am not quite sure how to find out what variable was zero and how you explain it, maybe you can explain?
• September 21st, 2012, 12:44 PM
Norm
Re: Whats Wrong? Beginning Code!
Call the println method to print out the variables used in the statement where the error happens:
System.out.println("yourVar="+ yourVar);

Replace yourVar with the name of the variable in your code.

Print out the values of all the variables and expressions that are used as divisors (what is to the right of the / operator). For example with this: 12/(a+b) you should print out (a+b)
• September 21st, 2012, 12:53 PM
LoganC
Re: Whats Wrong? Beginning Code!
custNameLogan
annualInterest0.05

Is what is being printed for custName
and annualInterest is printing .05

So it's not reading 0 exactly, what else should I try.
• September 21st, 2012, 12:56 PM
LoganC
Re: Whats Wrong? Beginning Code!
Logan
What is the price of the car? 150000
What is the downpayment? 500
What is the trade-in value? 1000
For how many months is the loan? 60
What is the decimal interest rate? .05
price=150000
Exception in thread "main" java.lang.ArithmeticException: / by zero
downPayment=500
months=60
at bert.calculatePayment(bert.java:63)
annualInterest=0.05
at bert.main(bert.java:46)
custName=Logan
Java Result: 1

I cannot see where anything is equalling zero, so what else should I check on Norm? And thank you for this information so far.
• September 21st, 2012, 01:17 PM
Norm
Re: Whats Wrong? Beginning Code!
Did you print out the values of all the expressions that are used as divisors (what is to the right of the / operator). For example with this: 12/(a+b) you should print out (a+b)

Where do you print out the values of all the variables used in this statement:
Code :

`loanAmt/((1/interest)-(1/(interest*Math.pow(1+interest,months))));`
especially those to the right of any /s?
• September 21st, 2012, 01:26 PM
LoganC
Re: Whats Wrong? Beginning Code!
I have printed out everything that has to do with this program and I don't see anything that is zero.
• September 21st, 2012, 01:27 PM
Norm
Re: Whats Wrong? Beginning Code!
Where do you print out the values of all the variables used in this statement:
Code :

`loanAmt/((1/interest)-(1/(interest*Math.pow(1+interest,months))));`
especially those to the right of any /s?

The ONLY important values for this error are those to the right of any /s. That's where the zero value is that is causing the error:
/ by zero
• September 21st, 2012, 06:21 PM
ChicoTheMan94
Re: Whats Wrong? Beginning Code!
This took me longer to figure out than I thought. Here's a question, why do you have interest as an int variable instead of a double? This code right here:
Code java:

`interest = (int)annualInterest / 12;`
is your problem area. When dividing you should probably be working with doubles because you are going to (probably) be working with decimal points! Especially since annualInterest is a double.

Take this for instance, you're annual interest comes from the question:
Code java:

``` System.out.print("What is the decimal interest rate? "); annualInterest = reader.nextDouble();```
If you put in 0.5 as the interest rate, but you aren't working with doubles, so it doesn't see the .5!
• September 21st, 2012, 06:45 PM
Norm
Re: Whats Wrong? Beginning Code!
Quote:

This took me longer to figure out than I thought.
@ChicoTheMan94 And now the OP will not have the pleasure of finding the problem himself, let alone the things he might learn about how to find problems in code.
• September 21st, 2012, 06:50 PM
LoganC
Re: Whats Wrong? Beginning Code!
Thanks a lot, both of you. I appreciate you Norm helping me educate myself, but I also thank Chico for helping me with a quick fix. This surely will educate me for the future. Thanks Chico and especially Norm for helping me ALL DAY.
• September 21st, 2012, 07:38 PM
ChicoTheMan94
Re: Whats Wrong? Beginning Code!
Quote:

Originally Posted by Norm
@ChicoTheMan94 And now the OP will not have the pleasure of finding the problem himself, let alone the things he might learn about how to find problems in code.

Ugh, I'm sorry. I'm trying to get the hang of this not spoon feeding I promise, I tried not to give any actual answers, and just be vague, guess I could have been more vague. I haven't been doing this for long.
• September 21st, 2012, 08:09 PM
Norm
Re: Whats Wrong? Beginning Code!
You gave a good answer, but I'm not sure the OP ever figured out what was causing the / by zero error. I don't know if he ever saw that the value of interest was 0, causing 1/interest to cause the error.
Once he found that out, then you've given a good explanation of why it was 0 and how to fix it.