Hey guys this is my first post and I kind of need the answer ASAP, as it is due by midnight tonight (yes I know, way to procrastinate right?). I am taking an online Java programming class and I have been struggling but I actually fully debugged this program... or so I thought. When I went to run it, it allows me to enter all my info, but after the last calculation, I get this error message(also pictured below):
"Exception in thread "main" java.lang.ArithmeticException: / by zero
at bert.Bert.main(Bert.java:40)
BUILD FAILED (total time: 18 seconds)"
Can anyone help please?? I have my code attached as 2 images, but in case you'd rather view it in text, here is my code:
package bert;
import java.io.*;
public class Bert {
public static void main(String[] args) throws IOException{
BufferedReader dataIn = new BufferedReader (new InputStreamReader(System.in));
//Declaring Variables
int price, downpayment, tradeIn, months,loanAmt=0, interest =0;
double annualInterest, payment;
String custName, inputPrice,inputDownPayment,inputTradeIn,inputMonths, inputAnnualInterest;
//Get Input from User
System.out.println("What is your name?");
custName = dataIn.readLine();
System.out.print("What is the price of the car?");
inputPrice = dataIn.readLine();
System.out.print("What is the downpayment?");
inputDownPayment = dataIn.readLine();
System.out.print("What is the trade-in value?");
inputTradeIn = dataIn.readLine();
System.out.print("For how many months is the loan?");
inputMonths = dataIn.readLine();
System.out.print("What is the decimal interest rate?");
inputAnnualInterest = dataIn.readLine();
//Conversions
price = Integer.parseInt(inputPrice);
downpayment = Integer.parseInt(inputDownPayment);
tradeIn = Integer.parseInt(inputTradeIn);
months = Integer.parseInt(inputMonths);
annualInterest =Double.parseDouble(inputAnnualInterest);
//Calculations
payment=loanAmt/((1/interest)-(1/(interest*Math.round(1+ months))));
//Output
System.out.print("The monthly payment for " + custName + " is $");
System.out.println((int)payment);
}
/**
* @param args the command line arguments
*/
"Exception in thread "main" java.lang.ArithmeticException: / by zero
at bert.Bert.main(Bert.java:40)
What variable has the zero value when line 40 is executed?
Can the code test for the zero and not try to divide by it?
Note: The images are too fuzzy when enlarged. Can you copy and paste line 40 here along with the preceding statements that show the values of all the variables used in line 40.
Be sure to wrap the code in code tags. Use the # button above the input box:
Code:
int x = 3;
Last edited by Norm; February 14th, 2017 at 05:48 PM.
When posting code, please use code tags as per Norm's post #2. Go Advanced, select the formatted code and click '#'.
Cheers!
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
PS loadAmt is set to 0 in line 17 but never updated!
Last edited by 2kaud; February 15th, 2017 at 07:51 AM.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.