Results 1 to 8 of 8
  1. #1
    Zexis is offline Member
    Join Date
    Dec 2011
    Posts
    10
    Rep Power
    0

    Default Errors with some basic while and for loops, help?

    Hello. For a class assignment, I have to write a program that finds the first four perfect numbers on the domain (0, ∞). A perfect number is a number that has the sum of its divisors (aside from the number itself) equivalent to the number. 6, for example, is a perfect number.

    Anyways, here's my code.

    Java Code:
    public class PerfectNumbers
     {
    
        public static void main(String[] args)
        {
    		int i = 1;
    		while (int i <= 4)
    		{
    			int j;
    			for (int j = 1; i <= 4; j++;)
    			{
    				int sum = 0;
    				int k = 1;
    
    				while (k <= j - 1)
    				{
    					if (j % k == 0)
    					{
    						sum += k;
    						k++;
    					}
    					else
    						k++;
    				}
    				if (sum == j)
    				{
    					System.out.println(j);
    					i++;
    				}
    			}
    		}
        }
    }
    I'm getting errors on lines 7, 10 and 33. I'm sure I'm making some novice mistakes...can someone please point me in the right direction?

    Thank you.
    Last edited by Zexis; 12-01-2011 at 10:54 PM.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default Re: Errors with some basic while and for loops, help?

    When you get errors you should post the exact error message.

    Computers are very good at remembering things. Once you have told the computer what type a variable is you do not have to keep reminding it.

  3. #3
    Zexis is offline Member
    Join Date
    Dec 2011
    Posts
    10
    Rep Power
    0

    Default Re: Errors with some basic while and for loops, help?

    Oh, hey, taking out the two extra "int"s fixed most of my errors! Thank you.

    Now I'm left with two. Seems to be a problem with my for loop.

    On line 10, I'm getting a "')' expected" and "illegal start of expression"

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default Re: Errors with some basic while and for loops, help?

    Look very carefully on that line to see if anything that should be there isn't or if anything that is there shouldn't be there.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Errors with some basic while and for loops, help?

    [Edit]...

  6. #6
    Zexis is offline Member
    Join Date
    Dec 2011
    Posts
    10
    Rep Power
    0

    Default Re: Errors with some basic while and for loops, help?

    Thanks a lot guys. We just learned how to write for and while loops today in class, so I'm glad it was only a small syntax error.

    Now I just have to figure out why my program isn't printing anything. But I guess that's just a logic error...at least the compiler errors are gone. Thanks again.

  7. #7
    Zexis is offline Member
    Join Date
    Dec 2011
    Posts
    10
    Rep Power
    0

    Default Re: Errors with some basic while and for loops, help?

    Sorry for the double post, but I got my program working. Whoo! When I went back to edit it I had changed one of the less than signs to greater than. All is well now.

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default Re: Errors with some basic while and for loops, help?

    This can be achieved with only 2 loops. The outer loop goes until it finds the four perfect numbers. The inner loop calculates all the factors of a given number and sums them. After the inner loop have an if statement to see if the sum equals the number.

Similar Threads

  1. Replies: 2
    Last Post: 03-03-2011, 10:08 PM
  2. basic while loops
    By ts96 in forum New To Java
    Replies: 1
    Last Post: 02-16-2011, 12:10 PM
  3. Replies: 4
    Last Post: 01-04-2011, 12:36 AM
  4. Basic loops
    By Samari413 in forum New To Java
    Replies: 13
    Last Post: 11-14-2010, 01:19 AM
  5. For loops and arrays throwing up errors
    By Banjo in forum New To Java
    Replies: 2
    Last Post: 11-25-2008, 06:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •