Jenna Dijohn

Greenhorn

Posts: 2

posted 5 years ago

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:

Homework4.java:11: cannot find symbol

symbol : class IOexception

location: class Homework4

public static void main(String[] args) throws IOexception

^

Homework4.java:25: cannot find symbol

symbol : variable demoninator

location: class Homework4

for (number=1, demoninator=30; number<=30; number++, demoninator--)

^

Homework4.java:25: 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!

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:

Homework4.java:11: cannot find symbol

symbol : class IOexception

location: class Homework4

public static void main(String[] args) throws IOexception

^

Homework4.java:25: cannot find symbol

symbol : variable demoninator

location: class Homework4

for (number=1, demoninator=30; number<=30; number++, demoninator--)

^

Homework4.java:25: 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!

posted 5 years ago

- 1

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.

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

Greenhorn

Posts: 2

posted 5 years ago

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 you...an 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 it...you 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).

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 you...an 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 it...you 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

Marshal

Posts: 56599

172

posted 5 years ago

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

Don’t use tabs for indenting. I have added code tags, and you can see how much better your posting now looks