About to give up

K

KyoGaSuki

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
 
M

Mark Space

KyoGaSuki said:
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.
 
R

rossum

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
 
L

Lew

rossum said:
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.
 
A

Andreas Leitgeb

KyoGaSuki said:
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.
 
A

Arved Sandstrom

KyoGaSuki said:
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
 
L

Lew

Arved said:
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.
 
A

Arved Sandstrom

Lew said:
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
 
M

Martin Gregorie

Arved said:
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.
 
E

EJP

Arved said:
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.
 
J

John W. Kennedy

EJP said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

While loops 4

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top