programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Fairly new to java wondering if I could get some help.

Tim Mannion
Greenhorn
Posts: 7
So I decided I wanted to teach myself to be a computer program with little to none prior experience. I borrowed a text book one of my friends had (Java Programming 5th ed by Joyce Farrell) and I'm working my way through it. I'm on chapter 6 and I'm stuck on a problem......I just learned how to make loops in this chapter and the problem asks me to print every perfect number from 1 - 1000 and explains that a perfect number is a number where all the numbers that divide evenly into it add up to the original number (like 6 and 28) I worked out a code that I think should work but it's not printing anything (it compiles with no error). Can anyone tell me what I'm doing wrong? here's my code so far:

EDIT: it wont let me format it with my indentations but you should be able to read that anyway i would think

(added by moderator Jesper: You can use code tags to get your source code nicely formatted. I've added them for you above).

Tina Smith
Ranch Hand
Posts: 208
9
Why don't you try the following:
Go through the numbers 1-10 and figure out which are perfect numbers, which are not, and what the sum is in all cases.

Add a println (System.out.println) statement after your second for loop (and anywhere else you might want to inspect the value of variables) to see what the variables you're trying to compare as equal (sum and x) actually are. I'd also decrease the size of your loop from 1000 to something like 10 to make it easier to debug, when you get it working with a smaller subset then you can expand it to 1000.

Compare your expected values with what's printed by the println statements.

Paul Clapham
Sheriff
Posts: 22834
43
Tim Mannion wrote:explains that a perfect number is a number where all the numbers that divide evenly into it add up to the original number (like 6 and 28)

That's right, that's what it is. But your code doesn't involve division or remainders in any way; this is a big hint about why it isn't producing the output you want.

Jeff Verdegan
Bartender
Posts: 6109
6
Bear Bibeault wrote:Please click this link ⇒ UseCodeTags ⇐

Also this one: UseAMeaningfulSubjectLine

Campbell Ritchie
Marshal
Posts: 56570
172
. . . and welcome to the Ranch

Michael Dunn
Ranch Hand
Posts: 4632
> So I decided I wanted to teach myself to be a computer program ...

you were enthralled by the movie Tron?

Tim Mannion
Greenhorn
Posts: 7
Thanks for the help from everyone. This forum seems really awesome and helpful but I'm still having some trouble with this problem

Paul Clapham wrote:
That's right, that's what it is. But your code doesn't involve division or remainders in any way; this is a big hint about why it isn't producing the output you want.

Thanks for that tid bit I totally felt like a fool when I realized I didn't include any % statement but now I added one and it still won't print anything

Matthew Brown
Bartender
Posts: 4568
9
You might want to think about where you're setting (and resetting?) sum to zero.

Edit: what you really ought to have tried is temporarily getting rid of the if(sum == x) statement, and adding:
and it would probably have been obvious what was going wrong.

Tim Mannion
Greenhorn
Posts: 7
THANK YOU ALL SO MUCH FOR THE ADVICE! Here's my final code and it seems to be working fine. Thanks Matt for pointing out that I forgot to reset my variables!