Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    New Coder
    Join Date
    Oct 2003
    Location
    Wisconsin
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Need help with Java Assignment

    Hi,

    I have tried every way I could think to correct this problem. This is the last of 13 errors I had to find debugging this assignment.

    here is the line of code:

    interest = annualInterest 12;

    after compiling it, the error message said:

    possible loss of precision

    found: double

    required: int

    the declaring variables are

    int price, downPayment, tradeIn, months, loanAmt, interest;
    double annualInterest, payment;

    under conversions there is this:

    annualInterest =Double.parseDouble(inputAnnualInterest);

    under calculations the line of code is

    interest = annualInterest 12;

    I tried putting "/" in front of 12, got an error. Tried putting parantheses around
    (annualInterest 12) got an error. How to get int when there is a double. I determined from declared variable annualInterest was the double. So here is what I tried next:

    interest = (double = (annualInterest / 12)); got an error back

    unexpected type
    required: variable
    found : class
    interest = (double = (annualInterest / 12));
    ^(under the d in double)
    1 error


    My understanding is everything in the ( ) has to equate to an int and not double...since interest is an int. Must be close because the error message no longer says found: double required: int. It just says found:class required variable.

    Maybe someone can clarify my understanding.

  • #2
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Code:
    interest = annualInterest / 12;
    Think to yourself what are you DOING with interest. How do you get it. You weren't really doing anything to it. The interest might be better off as a double. In the future post all of your code.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #3
    New Coder
    Join Date
    Oct 2003
    Location
    Wisconsin
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Got an error again

    C:\Documents and Settings\Owner\My Documents\Java\Chapter03\Bert.java:43: possible loss of precision
    found : double
    required: int
    interest = annualInterest / 12;
    ^(under division symbol)
    1 error

    Tool completed with exit code 1

    here is the total code, sorry for omitting it before.

    /*
    Chapter 3 Debugging Assignment
    Programmer: Shannon Chachere-Mindingall
    Date: May 8, 2006
    Program Name: Bert,java
    Purpose: The purpose of this project is to calculated the monthly payments for a car.
    */

    import java.io.*;

    public class Bert
    {
    public static void main(String[] args)throws IOException
    {
    //Declaring Variables
    int price, downPayment, tradeIn, months, loanAmt, interest;
    double annualInterest, payment;
    String custName, inputPrice,inputDownPayment,inputTradeIn,inputMonths, inputAnnualInterest;
    BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));

    //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
    interest = annualInterest / 12;
    loanAmt = price - downPayment - tradeIn;
    payment=loanAmt/((1/interest)-(1/(interest*Math.pow(1+interest,months))));

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

  • #4
    Regular Coder
    Join Date
    Mar 2006
    Location
    Ames, IA
    Posts
    132
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I beleive the issue is that you are assigning a double to an int. I am not sure about Java, but a double has more precision than an int, i.e. more space is allocated for doubles. This should really only be a warning, not an error, unless you have a really picky compiler. There are a couple ways to correct this: One, caste your annual interest as an int (not recommended), or change the declaration interest to be a double, and divide annual interest by 12.0 (the preferred method). Later, if you need to take interest as an int, you can.

  • #5
    New Coder
    Join Date
    Oct 2003
    Location
    Wisconsin
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile Thanks everyone, I just got it

    the code was:

    interest = annualInterest/12;

    I needed to put interest = (int) annualInterest/12;

    I knew it was something simple, I thought about that hours ago and let it slip.

    Thanks so much for your help.

    OneLuv

  • #6
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Hmm I wouldn't think you would need to type cast it.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #7
    Regular Coder
    Join Date
    Mar 2006
    Location
    Ames, IA
    Posts
    132
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I also wouldnt recommend doing it till after the calculation, as you willl lose precision. I would recommend changing interest to a double, do the calculation then caste it as an int, see my first post.

  • #8
    New Coder
    Join Date
    Oct 2003
    Location
    Wisconsin
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Yes the calculations

    Mind you I havent slept in 2 days. Tried to run the calculations, mind u the application compliled just fine.

    I got an exception error line 45 / zero ??

  • #9
    Gox
    Gox is offline
    Regular Coder Gox's Avatar
    Join Date
    May 2006
    Location
    Ontario, Canada
    Posts
    392
    Thanks
    2
    Thanked 20 Times in 20 Posts
    Quote Originally Posted by oneluvsdesire
    Mind you I havent slept in 2 days. Tried to run the calculations, mind u the application compliled just fine.

    I got an exception error line 45 / zero ??
    Looks like a divide by zero exception. What is the code on line 45?
    Without knowing the code for that exception here's two things you might want to look into.

    1) Re-work your calculations so that dividing by zero will never occur
    2) Before performing your calculations check that the dividing variable isn't zero.
    Example:
    Code:
    int x, y;
    //Some code to set x and y
    ...
    
    if(x != 0)
         //do calculation
         z = y / x;
    else
       //Inform the user calculation is not possible
      System.out.println("Calculation not possible. Division by Zero");

  • #10
    New Coder
    Join Date
    Oct 2003
    Location
    Wisconsin
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Can you dumb it down for me?

    This is a beginning Java class. The following code was pre-written, I just needed to debug it. I did get stuck on the "cast" issue, but I dont think the book gave an adequate explanation. So after 10 hours and bugging u guys, I winged it and it compiled. But from what Im reading did I do it wrong? To everyone who is helping me, my goal is to speak in the terms that yall are speaking, but its so greek to me. With corrected errors, this is what was there when it compiled.

    /*
    Chapter 3 Debugging Assignment
    Programmer: Shannon Chachere-Mindingall
    Date: May 8, 2006
    Program Name: Bert,java
    Purpose: The purpose of this project is to calculated the monthly payments for a car.
    */

    import java.io.*;

    public class Bert
    {
    public static void main(String[] args)throws IOException
    {
    //Declaring Variables
    int price, downPayment, tradeIn, months, loanAmt, interest;
    double annualInterest, payment;
    String custName, inputPrice,inputDownPayment,inputTradeIn,inputMonths, inputAnnualInterest;
    BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));

    //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
    interest = (int) annualInterest/12;
    loanAmt = price - downPayment - tradeIn;
    payment=loanAmt/((1/interest)-(1/(interest*Math.pow(1+interest,months))));

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


    Melon00 you previously posted: One, caste your annual interest as an int (not recommended), or change the declaration interest to be a double, and divide annual interest by 12.0 . Can you give me an example? I am assuming I did the no-no when I put (int) in front of the annualInterest. (would that be "casting"??) How would I go about the second part?



    The exception error:

  • #11
    New Coder
    Join Date
    Oct 2003
    Location
    Wisconsin
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Can you dumb it down for me?

    This is a beginning Java class. The following code was pre-written, I just needed to debug it. I did get stuck on the "cast" issue, but I dont think the book gave an adequate explanation. So after 10 hours and bugging u guys, I winged it and it compiled. But from what Im reading did I do it wrong? To everyone who is helping me, my goal is to speak in the terms that yall are speaking, but its so greek to me. With corrected errors, this is what was there when it compiled.

    /*
    Chapter 3 Debugging Assignment
    Programmer: Shannon Chachere-Mindingall
    Date: May 8, 2006
    Program Name: Bert.java
    Purpose: The purpose of this project is to calculated the monthly payments for a car.
    */

    import java.io.*;

    public class Bert
    {
    public static void main(String[] args) throws IOException
    {
    //Declaring Variables
    int price, downPayment, tradeIn, months, loanAmt, interest;
    double annualInterest, payment;
    String custName, inputPrice,inputDownPayment,inputTradeIn,inputMonths, inputAnnualInterest;
    BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));

    //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
    interest = (int) annualInterest/12;
    loanAmt = price - downPayment - tradeIn;
    payment=loanAmt/((1/interest)-(1/(interest*Math.pow(1+interest,months))));

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


    Melon00 you previously posted: One, caste your annual interest as an int (not recommended), or change the declaration interest to be a double, and divide annual interest by 12.0 . Can you give me an example? I am assuming I did the no-no when I put (int) in front of the annualInterest. (would that be "casting"??) How would I go about the second part? BTW im using TextPad for my lesson, does that make a difference?



    The exception error:

    Exception in thread "main" java.lang.ArithmeticException: / zero
    at Bert.main(Bert.java:45)

    All I know about exceptions is that they happen even if the app compiles, because of a programming error. The code line that is large and orange, is line 45.

    Gox I remember if then else statements from c++builder. Whatever the problem, no one else in my class had it, but then again they have programming experience. The chapter is dealing with manipulating Data using Methods. From what i read, looking at that line of code, I can't tell that something is wrong with it. Help!

  • #12
    Regular Coder brad211987's Avatar
    Join Date
    Sep 2005
    Location
    Ohio
    Posts
    631
    Thanks
    10
    Thanked 50 Times in 50 Posts
    Code:
    interest = annualInterest / 12;
    The problem with this, and with casting annualInterest as an int ((int) annualInterest) is precision. interest is an int variable, annualInterest is a double variable. Here is an example of what could happen as a result of this. Suppose we have an int variable x, and a double variable y. If y = 8.8, and you try to evaluate the statement x = y/2, java assigns 4 to x. This is because 8.8/2 = 4.4, however, when java assigns 4.4 to an int value, it truncates(chops off) the decimal portion. When looking at interest rates, this could result in a problem.

    casting the variable as an int, just performs the truncation before the calculation, yielding the same result.

    as for this part:
    Code:
    payment=loanAmt/((1/interest)-(1/(interest*Math.pow(1+interest,months))));
    The problem lies with this: ((1/interest)-(1/(interest*Math.pow(1+interest,months)))) , this part of the equation is evaluating to 0 when your program comes across it. I would recommend running through your program with pencil and paper and evaluating the expression yourself to see where the problem may be.(it may have to do with the possible truncation from the previous part)

    Anyway, hope this helps a little bit, good luck.

  • #13
    Regular Coder
    Join Date
    Mar 2006
    Location
    Ames, IA
    Posts
    132
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Brad is trying to tell you what I was trying to tell you a few posts ago. What we are saying is that when any number gets converted to an integer, the decimal is chopped off, e.g. 903.95 becomes 903. It is better to leave the decimal place when doing calculations, especially when dealing with money. This is just good programming practice in general, but cutting off 0.95 cents doesnt seem like much, but do it a few hundred times, to a few hundred clients, and now you have a problem.

    Since you say you are a beginner programmer, there is something you must know. You need, need, need, to test parts of your program before testing from start to finish. The best way to program is to break it up into sections, and test each section. This may seem a lot slower when you are coding, but you wont be doing any all-night debugging sections. If your program worked before adding a new section, it is the fault of that new section. Worst case scenerio you have to recode that part, but, big deal compared to trying to debug a problem in the amidst of hundreds of lines of code.

    I apologize for our speaking a language you dont know...believe me, I know what you are talking about, as a matter of fact, we all do. If someone uses a term that you dont know just google/dictionary.com the word. I still do this and I have years of Computer Engineering background. If it makes you feel better, you will never get to a point where you will understand everything and everyone says to you about programming or computer science in general.

  • #14
    New Coder
    Join Date
    Oct 2003
    Location
    Wisconsin
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Am I getting it?

    Hey yall, thanks so much. I went back deleted the (int) and moved the interest from the int to the double. Divided the annualInterest by 12.0 re compiled and ran the program. This time no exceptions. (yay!) But the answer came out as $297.22192989317324. Now how would I go back and cast the (int) and all those behind the decimal (because of the double)
    This is what I need to know, and I probably need to get some supplemental text because this book is not cutting it.

    1. How would I know from just looking at the error, that I should divide by 12.0? ( Its obvious to me that there is a difference even though 12 and 12.0 mean the same, just not in Java?)

    2. How would I know to go back and declare the interest as a double instead of an int?

    (Trust me, the text is not covering this at all! We are using Java Programming Comprehensive Concepts and Techniques, Shelly, Cashman Starks Mick) Can someone recommend a basic, basic book I can buy. Up until this point I was really enjoying this to the point I was deciding whether to pursue Networking or Programming. It doesn't matter how simple the information is, I need a better foundation than what this book is giving me.

    I need something to read that is going to tell me the "why" It was easy for you all to tell me what to change, but knowing why I will know to change it everytime I see it.

    Thanks so much for letting me pick your brains. Can't wait till I know enough to help someone else. You guys are the best!

  • #15
    Gox
    Gox is offline
    Regular Coder Gox's Avatar
    Join Date
    May 2006
    Location
    Ontario, Canada
    Posts
    392
    Thanks
    2
    Thanked 20 Times in 20 Posts
    Quote Originally Posted by oneluvsdesire
    ...But the answer came out as $297.22192989317324. Now how would I go back and cast the (int) and all those behind the decimal (because of the double) ...
    If all you want to do is print out everything before the decimal, you can "cast" the variable at printing time. The variable will still be a double (internally), but it will print as if it were an int. Here is the change you'd need to make.

    System.out.println((int)payment);

    EDIT: Since it's a money type of program, maybe you want keep 2 decimal places when you display the answer. There are a few different ways you could accomplish this.

    a) the printf method. This would be my preferred choice. However, it seems my version of Java doesn't like the method call...maybe it's a new feature. Here's a link to some usage http://www.java2s.com/ExampleCode/La...rateprintf.htm

    b) Using a Formatter, again here's a link on usage http://www.java2s.com/ExampleCode/La...esandcubes.htm

    c) Here's third option since my version or Java/compiler doesn't seem to like the other two.
    EDIT: Forgot the import for this option
    import java.text.*;
    Code:
    import java.text.*;
    double result = 123.456789;
    
    //The #'s specify that there is to be 3 numbers before the decimal, and two numbers after the decimal
    NumberFormat two = new DecimalFormat("###.##"); 
    
    System.out.println("Result with two decimal places: " + two.format(result));
    Last edited by Gox; 05-11-2006 at 05:03 AM.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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