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
int price, downPayment, tradeIn, months;
double annualInterest, payment;
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);
}
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;
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);
return;
}
}
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!
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.
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?
Please Edit your post and wrap your code with
[code=java]
<YOUR CODE HERE>
[/code]
to get highlighting and preserve formatting.
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!
Re: Whats Wrong? Beginning Code!
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 .
Re: Whats Wrong? Beginning Code!
Hey Norm, I just added my whole code in the post above.
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.
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?
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)
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.
Re: Whats Wrong? Beginning Code!
What is your first name?
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
tradeIn=1000
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.
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?
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.
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
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!
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.
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.
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.
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.