About to give up

Discussion in 'Java' started by KyoGaSuki, Mar 6, 2008.

  1. KyoGaSuki

    KyoGaSuki Guest

    So finally it decided to actually PRINT something...and then I
    realized that it wasn't working how I needed it to. I am just ready
    to give up completely considering it is due in an hour and a half and
    I have managed to mess it all up. That, and the formatting won't
    work, AND nothing is showing up in the output file. *is seriously
    close to tears*:


    /**
    * @(#)Try2.java
    *
    * Try2 application
    *
    * @author
    * @version 1.00 2008/3/6
    */
    import java.util.*;
    import java.io.*;
    import java.math.*;
    public class Try2 {

    public static void main(String[] args)throws FileNotFoundException
    {
    Scanner in = new Scanner (new FileReader("Annuities.txt"));
    int n = in.nextInt();
    PrintWriter out = new PrintWriter("Try2.txt");
    int counter = 1;
    while(in.hasNext()){
    int sbal = in.nextInt();
    double annual = sbal*.06;
    double interest = annual/12;
    int deposit = 200;
    double ebal = sbal + interest + deposit;
    System.out.printf("$%7.2f $%6.2f $%6.2f $
    %7.2f",sbal,interest,deposit,ebal);
    counter++;
    }
    in.close();
    out.close();
    }
    }


    RESULTS:
    --------------------Configuration: Try2 - JDK version 1.6.0_03
    <Default> - <Default>--------------------

    Process completed.



    It isn't even printing anything anymore.

    it is SUPPOSED to turn out like this:

    1 $ - $ - $200.00 $ 200.00
    2 $ 200.00 $ 1.00 $201.00 $ 402.00
    3 $ 402.00 $ 2.01 $202.00 $ 606.01
    4 $ 606.01 $ 3.03 $203.00 $ 812.04
    5 $ 812.04 $ 4.06 $204.00 $1,020.10
    6 $1,020.10 $ 5.10 $205.00 $1,230.20
    7 $1,230.20 $ 6.15 $206.00 $1,442.35
    8 $1,442.35 $ 7.21 $207.00 $1,656.56
    9 $1,656.56 $ 8.28 $208.00 $1,872.84
    10 $1,872.84 $ 9.36 $209.00 $2,091.20
    11 $2,091.20 $10.46 $210.00 $2,311.66
    12 $2,311.66 $11.56 $211.00 $2,534.22
     
    KyoGaSuki, Mar 6, 2008
    #1
    1. Advertising

  2. KyoGaSuki

    Mark Space Guest

    KyoGaSuki wrote:
    > So finally it decided to actually PRINT something...and then I
    > realized that it wasn't working how I needed it to. I am just ready
    > to give up completely considering it is due in an hour and a half and
    > I have managed to mess it all up. That, and the formatting won't
    > work, AND nothing is showing up in the output file. *is seriously
    > close to tears*:


    Well, I'm sorry you are having a rough time, and I'm sorry I don't have
    time to look at the program fully right now, but I did want to say that
    the only real mistake you can make is to give up.

    This goes for the rest of your professional career too. I've seen lots
    of programmers in the real world just give up. A problem is too hard to
    fix and they either implement a kludge or they just give up entirely and
    don't fix it. Good programmers don't give up, even if it takes them months.

    If you can't get this problem be sure to bring it to your teacher, and
    continue to work on it until you do understand the solution, even if you
    don't get a grade. It's most important to learn; that way the next
    problem is easier.
     
    Mark Space, Mar 6, 2008
    #2
    1. Advertising

  3. KyoGaSuki

    rossum Guest

    On Thu, 6 Mar 2008 11:42:23 -0800 (PST), KyoGaSuki
    <> wrote:

    >So finally it decided to actually PRINT something...and then I
    >realized that it wasn't working how I needed it to. I am just ready
    >to give up completely considering it is due in an hour and a half and
    >I have managed to mess it all up. That, and the formatting won't
    >work, AND nothing is showing up in the output file. *is seriously
    >close to tears*:
    >
    >
    >/**
    > * @(#)Try2.java
    > *
    > * Try2 application
    > *
    > * @author
    > * @version 1.00 2008/3/6
    > */
    > import java.util.*;
    > import java.io.*;
    > import java.math.*;
    >public class Try2 {
    >
    > public static void main(String[] args)throws FileNotFoundException
    >{
    > Scanner in = new Scanner (new FileReader("Annuities.txt"));
    > int n = in.nextInt();
    > PrintWriter out = new PrintWriter("Try2.txt");
    > int counter = 1;
    > while(in.hasNext()){
    > int sbal = in.nextInt();
    > double annual = sbal*.06;
    > double interest = annual/12;
    > int deposit = 200;
    > double ebal = sbal + interest + deposit;
    > System.out.printf("$%7.2f $%6.2f $%6.2f $
    >%7.2f",sbal,interest,deposit,ebal);

    You have an error in your print formats. You are printing four
    variables: sbal, interest, deposit and ebal. These are declared as
    int, double, int and double respectively. You are using the double
    print format (%#.#f) for all four. You should either use the integer
    print format (%#d) for the two integers or else make sure that the two
    values in question are doubles by multiplying them by 1.0.

    That is not your only problem, but it should get you some output to
    look at. The other problems are more obvious once you can see the
    output.

    rossum

    > counter++;
    > }
    > in.close();
    > out.close();
    > }
    >}
    >
    >
    >RESULTS:
    >--------------------Configuration: Try2 - JDK version 1.6.0_03
    ><Default> - <Default>--------------------
    >
    >Process completed.
    >
    >
    >
    >It isn't even printing anything anymore.
    >
    >it is SUPPOSED to turn out like this:
    >
    > 1 $ - $ - $200.00 $ 200.00
    > 2 $ 200.00 $ 1.00 $201.00 $ 402.00
    > 3 $ 402.00 $ 2.01 $202.00 $ 606.01
    > 4 $ 606.01 $ 3.03 $203.00 $ 812.04
    > 5 $ 812.04 $ 4.06 $204.00 $1,020.10
    > 6 $1,020.10 $ 5.10 $205.00 $1,230.20
    > 7 $1,230.20 $ 6.15 $206.00 $1,442.35
    > 8 $1,442.35 $ 7.21 $207.00 $1,656.56
    > 9 $1,656.56 $ 8.28 $208.00 $1,872.84
    > 10 $1,872.84 $ 9.36 $209.00 $2,091.20
    > 11 $2,091.20 $10.46 $210.00 $2,311.66
    > 12 $2,311.66 $11.56 $211.00 $2,534.22
     
    rossum, Mar 6, 2008
    #3
  4. KyoGaSuki

    Lew Guest

    rossum wrote:
    > You have an error in your print formats. You are printing four
    > variables: sbal, interest, deposit and ebal. These are declared as
    > int, double, int and double respectively. You are using the double
    > print format (%#.#f) for all four. You should either use the integer
    > print format (%#d) for the two integers or else make sure that the two
    > values in question are doubles by multiplying them by 1.0.


    Say, rather, by casting them to double. If what you want is to cast, it's
    silly to multiply when there exists a cast operator.

    --
    Lew
     
    Lew, Mar 7, 2008
    #4
  5. KyoGaSuki <> wrote:
    > PrintWriter out = new PrintWriter("Try2.txt");


    Here you created a PrintWriter, but ...

    > System.out.printf("$%7.2f $%6.2f $%6.2f $
    > %7.2f",sbal,interest,deposit,ebal);


    ....here you do *not* use it, but instead the
    System's standard-out channel!

    Also, printf doesn't write a linefeed by itself.
    add %n to the end of the format string to actually
    see separate lines.

    PS: the answers of the others do also apply.
     
    Andreas Leitgeb, Mar 7, 2008
    #5
  6. KyoGaSuki wrote:

    > So finally it decided to actually PRINT something...and then I
    > realized that it wasn't working how I needed it to. I am just ready
    > to give up completely considering it is due in an hour and a half and
    > I have managed to mess it all up. That, and the formatting won't
    > work, AND nothing is showing up in the output file. *is seriously
    > close to tears*:
    >

    Here's the guts of a small test file I wrote up just now:

    **********
    Scanner scanner = new Scanner(new File("test1.txt"));
    float balance = 1000.00f;
    while (scanner.hasNext()) {
    int num = scanner.nextInt();
    float amt = scanner.nextFloat();
    balance -= amt;

    System.out.printf("%3d\t%6.2f\n", num, balance);
    }
    scanner.close();
    **********

    The input file has an integer and a float on each line. A typical output
    line (console) is:

    4 848.93

    Some thoughts on your code:

    1. as others pointed out, why create the PrintWriter and not use it? No
    surprise that nothing is going to a file;
    2. the format descriptor for ints is 'd';
    3. You're talking money here...why are 'balance' and 'deposit' not floats or
    doubles?;
    4. catch the FileNotFoundException and do something useful with it *in* your
    app, even if at this stage you are just printing a message.

    I'll assume that the format of your input file supports your Scanner
    parsing.

    AHS
    --
    * change 'two' to '2' to email me
     
    Arved Sandstrom, Mar 9, 2008
    #6
  7. KyoGaSuki

    Lew Guest

    Arved Sandstrom wrote:
    > 3. You're talking money here...why are 'balance' and 'deposit' not floats or
    > doubles?;


    float is a terrible type for monetary values, and double not much better. One
    uses double for certain types of calculations, but precision and accuracy
    become limiting factors. For routine arithmetic calculations an integral type
    (long may be better than int) or Number type, BigDecimal or BigInteger, could
    be much better.

    The problem wasn't so much that 'deposit' and 'sbal' were int values, but that
    everything else was in double.

    Since the OP's problem had to do with interest calculations, there is a
    temptation to use double here, although BigDecimal certainly has the chops to
    do the job.

    --
    Lew
     
    Lew, Mar 9, 2008
    #7
  8. "Lew" <> wrote in message
    news:...
    > Arved Sandstrom wrote:
    >> 3. You're talking money here...why are 'balance' and 'deposit' not floats
    >> or
    >> doubles?;

    >
    > float is a terrible type for monetary values, and double not much better.
    > One uses double for certain types of calculations, but precision and
    > accuracy become limiting factors. For routine arithmetic calculations an
    > integral type (long may be better than int) or Number type, BigDecimal or
    > BigInteger, could be much better.


    It's hard to say. I understand that plenty of financial transactions are
    routinely done using floating point. After all, it's all well and good to
    stay exact when adding or subtracting, but as soon as you start dividing or
    doing interest you're no longer exact anyway.

    > The problem wasn't so much that 'deposit' and 'sbal' were int values, but
    > that everything else was in double.


    Agreed.

    > Since the OP's problem had to do with interest calculations, there is a
    > temptation to use double here, although BigDecimal certainly has the chops
    > to do the job.


    Double would probably be fine, I would think.

    AHS
     
    Arved Sandstrom, Mar 10, 2008
    #8
  9. Arved Sandstrom wrote:
    > "Lew" <> wrote in message
    > news:...
    >> Arved Sandstrom wrote:
    >>> 3. You're talking money here...why are 'balance' and 'deposit' not floats
    >>> or
    >>> doubles?;

    >> float is a terrible type for monetary values, and double not much better.
    >> One uses double for certain types of calculations, but precision and
    >> accuracy become limiting factors. For routine arithmetic calculations an
    >> integral type (long may be better than int) or Number type, BigDecimal or
    >> BigInteger, could be much better.

    >
    > It's hard to say. I understand that plenty of financial transactions are
    > routinely done using floating point. After all, it's all well and good to
    > stay exact when adding or subtracting, but as soon as you start dividing or
    > doing interest you're no longer exact anyway.
    >

    Also multiplication and division with the rounding conventions stated,
    with values held as integers using the so-called "binary cents"
    convention (e.g $1.50 is held internally as 150 cents).

    More complex calculations, e.g. currency conversions, have very precise
    calculation rules laid down by the institutions that hand out one
    currency in exchange for another. Rounding errors, even if conversion
    rates have six decimal places, become important as the amounts get very
    large.

    There is a good reason why COBOL uses integers for monetary amounts and
    some dialects don't support floating point at all.

    > Double would probably be fine, I would think.
    >

    ....except that constraining its rounding and truncation behavior
    to match the mandated calculation procedures may be a pain, especially
    if they're written round integer arithmetic. BigDecimal has the edge
    under those circumstances.


    --
    martin@ | Martin Gregorie
    gregorie. | Essex, UK
    org |
     
    Martin Gregorie, Mar 11, 2008
    #9
  10. KyoGaSuki

    EJP Guest

    Arved Sandstrom wrote:
    > I understand that plenty of financial transactions are
    > routinely done using floating point.

    Possibly, but that doesn't make it correct. I've seen too many people
    slaving over 'why doesn't 0.99+0.01=1.00 in FP?' to believe that it's
    anything but a waste of time.
     
    EJP, Mar 12, 2008
    #10
  11. EJP wrote:
    > Arved Sandstrom wrote:
    >> I understand that plenty of financial transactions are routinely done
    >> using floating point.

    > Possibly, but that doesn't make it correct. I've seen too many people
    > slaving over 'why doesn't 0.99+0.01=1.00 in FP?' to believe that it's
    > anything but a waste of time.


    FP != binary.

    --
    John W. Kennedy
    "Compact is becoming contract,
    Man only earns and pays."
    -- Charles Williams. "Bors to Elayne: On the King's Coins"
     
    John W. Kennedy, Mar 15, 2008
    #11
  12. "John W. Kennedy" <> wrote in message
    news:47db33f1$0$25058$...
    > EJP wrote:
    >> Arved Sandstrom wrote:
    >>> I understand that plenty of financial transactions are routinely done
    >>> using floating point.

    >> Possibly, but that doesn't make it correct. I've seen too many people
    >> slaving over 'why doesn't 0.99+0.01=1.00 in FP?' to believe that it's
    >> anything but a waste of time.

    >
    > FP != binary.


    That's quite true. For example, 0.99 + 0.01 does equal 1.00 in decimal
    floating point.

    AHS
     
    Arved Sandstrom, Mar 16, 2008
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marcelo Birnbach [MS]
    Replies:
    0
    Views:
    3,330
    Marcelo Birnbach [MS]
    Jun 28, 2003
  2. biteside
    Replies:
    0
    Views:
    4,370
    biteside
    Oct 13, 2003
  3. Edison
    Replies:
    2
    Views:
    1,641
    Jonathan Bromley
    Jun 30, 2003
  4. senthil
    Replies:
    0
    Views:
    622
    senthil
    Nov 21, 2003
  5. grocery_stocker
    Replies:
    10
    Views:
    633
    Keith Thompson
    May 25, 2005
Loading...

Share This Page