CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Java debuggung

  1. #1
    Join Date
    Feb 2017
    Posts
    3

    Unhappy Java debuggung

    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
    */

    // TODO code application logic here
    }
    Attached Images Attached Images    

  2. #2
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: Java debuggung

    "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.
    Norm

  3. #3
    Join Date
    Feb 2017
    Posts
    3

    Re: Java debuggung

    Sorry about the photos! Here is my code with numbered lines.

    Code:
    14 public static void main(String[] args) throws IOException{
    15     BufferedReader dataIn = new BufferedReader (new InputStreamReader(System.in));
    16     //Declaring Variables
    17     int price, downpayment, tradeIn, months,loanAmt=0, interest =0;
    18     double annualInterest, payment;
    19     String custName, inputPrice,inputDownPayment,inputTradeIn,inputMonths, inputAnnualInterest;
    20     //Get Input from User
    21     System.out.println("What is your name?");
    22     custName = dataIn.readLine();
    23     System.out.print("What is the price of the car?");
    24     inputPrice = dataIn.readLine();
    25     System.out.print("What is the downpayment?");
    26     inputDownPayment = dataIn.readLine();
    27     System.out.print("What is the trade-in value?");
    28     inputTradeIn = dataIn.readLine();
    29     System.out.print("For how many months is the loan?");
    30     inputMonths = dataIn.readLine();
    31     System.out.print("What is the decimal interest rate?");
    32     inputAnnualInterest = dataIn.readLine();
    33     //Conversions
    34     price = Integer.parseInt(inputPrice);
    35     downpayment = Integer.parseInt(inputDownPayment);
    36     tradeIn = Integer.parseInt(inputTradeIn);
    37     months = Integer.parseInt(inputMonths);
    38     annualInterest =Double.parseDouble(inputAnnualInterest);
    39     //Calculations
    40     payment=loanAmt/((1/interest)-(1/(interest*Math.round(1+ months))));
    41     //Output
    42     System.out.print("The monthly payment for " + custName + " is $");
    43     System.out.println((int)payment);
    44 }
    Last edited by 2kaud; February 15th, 2017 at 04:22 AM. Reason: Added code tags

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,728

    Re: Java debuggung

    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!

    C++23 Compiler: Microsoft VS2022 (17.4.4)

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,728

    Re: Java debuggung

    Code:
     payment=loanAmt/((1/interest)-(1/(interest*Math.round(1+ months))));
    You use interest here which is set to 0 in line 17, but here
    Code:
     annualInterest =Double.parseDouble(inputAnnualInterest);
    you set annualInterest!

    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!

    C++23 Compiler: Microsoft VS2022 (17.4.4)

  6. #6
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: Java debuggung

    What variable has the zero value when line 40 is executed?
    Norm

  7. #7
    Join Date
    Feb 2017
    Posts
    3

    Re: Java debuggung

    Thanks so much!! I am very very new to Java and have much to learn. Thank you for your help!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured