File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes for Loops with PrintWriter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "for Loops with PrintWriter" Watch "for Loops with PrintWriter" New topic

for Loops with PrintWriter

Jenna Dijohn

Joined: Mar 29, 2012
Posts: 2
I'm in a basic Java class and I'm having trouble figuring out how to write the code for this program:

Write a for loop that calculates the total of the following series of numbers:
1/30 + 2/29 + 3/28 + ......+ 30/1

Using PrintWriter to output your result to file "result.txt".

This is what I have so far:

When I try to compile it, I get these errors: cannot find symbol
symbol : class IOexception
location: class Homework4
public static void main(String[] args) throws IOexception
^ cannot find symbol
symbol : variable demoninator
location: class Homework4
for (number=1, demoninator=30; number<=30; number++, demoninator--)
^ cannot find symbol
symbol : variable demoninator
location: class Homework4
for (number=1, demoninator=30; number<=30; number++, demoninator--)
3 errors

Every time I try to fix it, I get new errors. Can anyone help me out with this? Thanks so much in advance!
Paul Clapham

Joined: Oct 14, 2005
Posts: 18541

Yeah, "denominator" is a hard word to spell right, especially when you are typing it, and sure enough you misspelled it once. In that line which the error message is pointing to, of course. I find that copying and pasting is both faster and more reliable in a situation like that.

Edit... and "IOexception"? Java is a case-sensitive language, so "IOexception" and "IOException" are different things as far as Java is concerned. (Notice the capital E in the correct version and the lower-case e in your incorrect version.) Picky, picky, for sure, but that's the problem there.
Jenna Dijohn

Joined: Mar 29, 2012
Posts: 2
Thanks!! How should I edit it so it will calculate the total series of numbers?
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11168

You need to think about what exactly you want it to do. The best way to do that is to turn your computer off, get a pencil and some paper - and probably an eraser. Start writing down how YOU would do this. How would you calculate the total series of number? What would you need to be able to do, what would you need to remember, what can you forget after you get to various points...

I will give you a tip you may not be aware of...In many computer languages including java, and integer divided by an integer will give integer. 1 divided by 3 in java is 0. 20 divided by 3 is 6. Even if you store the result in a double (like your 'total' variable), you will only get integers added up. You need to force one value to be a floating point type, something like this:

total += (1.0 * number)/ denominator;

Also...if you think about don't need a separate counter for the numerator and the denominator. the sum of both are always 31. 1/30 is the same as 1/(31 -1). 2/29 is the same as 2/(31 - 2)...30/1 is the same as 30/(31 - 30).

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 38045
Welcome to the Ranch

Don’t use tabs for indenting. I have added code tags, and you can see how much better your posting now looks
jQuery in Action, 2nd edition
subject: for Loops with PrintWriter
Similar Threads
File Letter Counter help
Add an exception for divide by zero to my fraction class
compile time errors, why?
constructor problems
Need help writing star pattern to file