Page 2 of 2 FirstFirst 12
Results 21 to 37 of 37
  1. #21
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,267
    Rep Power
    25

    Default

    Look at post#2. That gives a basic idea. It shows you how the value of var changes each time thru the loop.
    The expression you use must include the old value of balance on the right side of the assignment operator (=)

    You should have only one value for balance. The current one. new and new new and new new new are not the way to go.

  2. #22
    louiedogg418 is offline Member
    Join Date
    Jun 2011
    Posts
    52
    Rep Power
    0

    Default

    should i have an "if" statement?? i just seems like my loop is missing something that will keep it running until the (year*12) is <=0

  3. #23
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,267
    Rep Power
    25

    Default

    should i have an "if" statement?
    I don't know what for.
    What statements would you want to conditionally execute?

  4. #24
    louiedogg418 is offline Member
    Join Date
    Jun 2011
    Posts
    52
    Rep Power
    0

    Default

    Java Code:
    for(int month=0;month<=(years*12);month++)
    
      {interest=loan*interest_mo;
      System.out.println("interest "+interest);
    
      principal=payments-interest;
      System.out.println("principal "+ principal);
    
      balance=loan-principal;
      System.out.println("balance "+ balance);
    
       balance=balance-principal;
      System.out.println("old balance "+balance);
    
      new_balance=balance-principal;
      System.out.println("new_balance " +new_balance);
    ok so is this what you mean by changing the value? but im still getting the same exact results?!

  5. #25
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,267
    Rep Power
    25

    Default

    Here is post#2 in executable code with its results:
    Java Code:
          int value = 30;
          for(int i=0; i < 5; i++) {
            int changeBy  = 4;     // Here compute amount to change value by I've hardcoded it at 4
            value = value - changeBy;  // reduce value of value
            System.out.println("i=" + i + ", value= " + value);
          }
    /*
    i=0, value= 26
    i=1, value= 22
    i=2, value= 18
    i=3, value= 14
    i=4, value= 10
    */

  6. #26
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,267
    Rep Power
    25

    Default

    Java Code:
       balance=loan-principal;
    
       balance=balance-principal;
    Explain what each of these two statements are doing.
    What is the value of balance when they're done?

  7. #27
    louiedogg418 is offline Member
    Join Date
    Jun 2011
    Posts
    52
    Rep Power
    0

    Default

    ahhhh!! so i have to set my balance before the loop!!! Wowza! something so lil can make a BIG difference!! Thank you sooo much!!

  8. #28
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,267
    Rep Power
    25

    Default

    Sorry to drag this out so far. I was hoping you'd get the AH HA

  9. #29
    louiedogg418 is offline Member
    Join Date
    Jun 2011
    Posts
    52
    Rep Power
    0

    Default

    hahah well thats how we learn! and now i feel accomplished!! :D it would not have been gratifying if you just told me, so thank you for your patience! so now the loop is reducing each time, but not hitting zero.... I think i need to tweak the calculations, but i think i can get it from here! thank you again!

  10. #30
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,267
    Rep Power
    25

    Default

    Some times taking a piece of paper and playing computer and writing down the values of variables as you go thru a loop helps. Here you used printlns to show them.
    OK. Great on to the next challenge.

  11. #31
    louiedogg418 is offline Member
    Join Date
    Jun 2011
    Posts
    52
    Rep Power
    0

    Default

    ok next challenge: So after my loop runs the very last out put is :

    balance 8015.652267446069
    balance 6886.914888340483
    balance 5752.768975960016
    balance 4613.18861441606
    balance 3468.147763639705
    balance 2317.6202587867133
    balance 1161.5798096396345
    balance 7.059270501486026E-9

    So my next question is: why isnt it 0, or why is it a negative exponent?

    here is the code:
    Java Code:
     balance=loan;
      
      for(int month=0;month<(years*12);month++)
    
      {
      	interest=balance*interest_mo;
      //System.out.println("interest "+interest);
    
      principal=payments-interest;
      //System.out.println("principal "+ principal);
    
      balance=balance-principal;
      System.out.println("balance "+ balance);

  12. #32
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,267
    Rep Power
    25

    Default

    It looks like a very small number. Since you are working with doubles, computations will not be nice and even numbers like they are when you use integers. It is not recommended to use doubles when working with money because of this problem. I can't remember what class is recommended.
    If you use a DecimalFormat class object to format the output your values can be printed to the nearest cent.
    Search on the forum for code samples for using DecimalFormat.
    Also you could search for currency or money to find other threads where the use of doubles for money is discussed.

  13. #33
    louiedogg418 is offline Member
    Join Date
    Jun 2011
    Posts
    52
    Rep Power
    0

    Default

    sweet! yea i did a string.format %.2f and it came out to zero!

  14. #34
    louiedogg418 is offline Member
    Join Date
    Jun 2011
    Posts
    52
    Rep Power
    0

    Default

    Ok so here is a new question: So lets say i want to printtln("Payment");
    fallowed by println("Balance is"); but increment the payment inside the first print line to look like "Payment 1 Balance is: " + balance, but for each time thru the loop
    increment payment 1 to payment 2?

  15. #35
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,267
    Rep Power
    25

    Default

    Use String concatenation:
    "some text " + i + " some more text" ...

    Where i is the loop variable. If you need to add 1 to i, put it in parans: ...+ (i+1) + ...
    The compiler will reduce the expression inside the () to a number before doing the concatenation.
    Otherwise the compiler will get confused. Try it without the ()s to see.

  16. #36
    louiedogg418 is offline Member
    Join Date
    Jun 2011
    Posts
    52
    Rep Power
    0

    Default

    ahhh sweet! that works out like a champ!

  17. #37
    jprimo is offline Member
    Join Date
    Mar 2012
    Posts
    1
    Rep Power
    0

    Default Re: About to pull my hair out!!

    Quote Originally Posted by louiedogg418 View Post
    ok next challenge: So after my loop runs the very last out put is :

    balance 8015.652267446069
    balance 6886.914888340483
    balance 5752.768975960016
    balance 4613.18861441606
    balance 3468.147763639705
    balance 2317.6202587867133
    balance 1161.5798096396345
    balance 7.059270501486026E-9

    So my next question is: why isnt it 0, or why is it a negative exponent?

    here is the code:
    Java Code:
     balance=loan;
      
      for(int month=0;month<(years*12);month++)
    
      {
      	interest=balance*interest_mo;
      //System.out.println("interest "+interest);
    
      principal=payments-interest;
      //System.out.println("principal "+ principal);
    
      balance=balance-principal;
      System.out.println("balance "+ balance);
    Actually, the number is correct (or close enough) the number you are seeing is scientific notation (also known as E notation) to indicate that the program is unable to display the number of decimals. E-9 has something to do with the exponential of the number. Your calculator will show you a similar result if you perform operations beyond the capability of the device. You can find a quick note on this on wikipedia or visit: E notation
    I ran into the same problem, but I noticed it was not a regular number... the E-number at the end is a big hint :-).

    By the way, any formatting in Java would have taken care of the problem :-).

Page 2 of 2 FirstFirst 12

Similar Threads

  1. About to pull my hair out. :(
    By cfox10 in forum New To Java
    Replies: 8
    Last Post: 04-05-2011, 05:03 AM
  2. Junit. Pulling my HAIR OUT!
    By phil128 in forum Eclipse
    Replies: 2
    Last Post: 10-04-2010, 07:06 PM
  3. Replies: 0
    Last Post: 11-15-2008, 07:29 AM

Posting Permissions

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