Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > About to give up

Reply
Thread Tools

About to give up

 
 
KyoGaSuki
Guest
Posts: n/a
 
      03-06-2008
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
 
Reply With Quote
 
 
 
 
Mark Space
Guest
Posts: n/a
 
      03-06-2008
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.
 
Reply With Quote
 
 
 
 
rossum
Guest
Posts: n/a
 
      03-06-2008
On Thu, 6 Mar 2008 11:42:23 -0800 (PST), KyoGaSuki
<(E-Mail Removed)> 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


 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      03-07-2008
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
 
Reply With Quote
 
Andreas Leitgeb
Guest
Posts: n/a
 
      03-07-2008
KyoGaSuki <(E-Mail Removed)> 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.
 
Reply With Quote
 
Arved Sandstrom
Guest
Posts: n/a
 
      03-09-2008
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
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      03-09-2008
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
 
Reply With Quote
 
Arved Sandstrom
Guest
Posts: n/a
 
      03-10-2008
"Lew" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed). ..
> 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



 
Reply With Quote
 
Martin Gregorie
Guest
Posts: n/a
 
      03-11-2008
Arved Sandstrom wrote:
> "Lew" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed). ..
>> 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 |
 
Reply With Quote
 
EJP
Guest
Posts: n/a
 
      03-12-2008
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
We dont give only job... We give you the Job Satisfaction....... Anuj Digital Photography 0 01-02-2007 06:35 PM
GIVE ME FILM OR GIVE ME DEATH l#vfgsgEg@AO1.com DVD Video 4 07-14-2005 03:10 PM
Why wouldn't this line of give give the address of the pointer? grocery_stocker C Programming 10 05-25-2005 03:05 AM
Give us 3 minutes; we give you the whole library lib Computer Support 1 02-04-2005 03:16 AM
Give us 3 minutes; we give you the whole library lib Computer Support 0 01-27-2005 07:52 AM



Advertisments