Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 20 of 20

Thread: Whats Wrong? Beginning Code!

  1. #1
    Member
    Join Date
    Sep 2012
    Posts
    98
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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;
    }
    }


  2. #2
    Junior Member
    Join Date
    Sep 2012
    Posts
    26
    My Mood
    Bored
    Thanks
    6
    Thanked 5 Times in 4 Posts

    Default 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:
    interest = annualInterest/12;
    because you could end up working with decimals, however interest is an int variable. Also in you're calculations part
    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!

  3. #3
    Member
    Join Date
    Sep 2012
    Posts
    98
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,414
    Thanks
    49
    Thanked 2,214 Times in 2,187 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Sep 2012
    Posts
    98
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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!


    /***
     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!
    Last edited by LoganC; September 21st, 2012 at 08:13 AM.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,414
    Thanks
    49
    Thanked 2,214 Times in 2,187 Posts

    Default Re: Whats Wrong? Beginning Code!

    / 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 .
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Sep 2012
    Posts
    98
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Whats Wrong? Beginning Code!

    Hey Norm, I just added my whole code in the post above.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,414
    Thanks
    49
    Thanked 2,214 Times in 2,187 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Sep 2012
    Posts
    98
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,414
    Thanks
    49
    Thanked 2,214 Times in 2,187 Posts

    Default 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)
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Sep 2012
    Posts
    98
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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.

  12. #12
    Member
    Join Date
    Sep 2012
    Posts
    98
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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.
    Last edited by LoganC; September 21st, 2012 at 01:15 PM.

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,414
    Thanks
    49
    Thanked 2,214 Times in 2,187 Posts

    Default 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:
    loanAmt/((1/interest)-(1/(interest*Math.pow(1+interest,months))));
    especially those to the right of any /s?
    Last edited by Norm; September 21st, 2012 at 01:26 PM.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #14
    Member
    Join Date
    Sep 2012
    Posts
    98
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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.

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,414
    Thanks
    49
    Thanked 2,214 Times in 2,187 Posts

    Default Re: Whats Wrong? Beginning Code!

    Where do you print out the values of all the variables used in this statement:
    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
    If you don't understand my answer, don't ignore it, ask a question.

  16. #16
    Junior Member
    Join Date
    Sep 2012
    Posts
    26
    My Mood
    Bored
    Thanks
    6
    Thanked 5 Times in 4 Posts

    Default 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:
    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:
            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!

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,414
    Thanks
    49
    Thanked 2,214 Times in 2,187 Posts

    Default Re: Whats Wrong? Beginning Code!

    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.
    If you don't understand my answer, don't ignore it, ask a question.

  18. #18
    Member
    Join Date
    Sep 2012
    Posts
    98
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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.

  19. #19
    Junior Member
    Join Date
    Sep 2012
    Posts
    26
    My Mood
    Bored
    Thanks
    6
    Thanked 5 Times in 4 Posts

    Default Re: Whats Wrong? Beginning Code!

    Quote Originally Posted by Norm View Post
    @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.

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,414
    Thanks
    49
    Thanked 2,214 Times in 2,187 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. whats wrong with my code.
    By jove in forum Object Oriented Programming
    Replies: 3
    Last Post: July 30th, 2011, 11:45 PM
  2. Whats wrong with my code!!
    By nitwit3 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 22nd, 2011, 11:45 AM
  3. Whats Wrong with this code?
    By whattheeff in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 9th, 2011, 09:59 PM
  4. Whats wrong with my code?
    By whattheeff in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 4th, 2011, 04:34 PM
  5. Whats wrong with my code?
    By mlan in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 27th, 2010, 12:42 PM