Results 1 to 5 of 5
  1. #1
    killertofu is offline Member
    Join Date
    Apr 2010
    Posts
    2
    Rep Power
    0

    Default Perfect/Deficient/Abundant Nums problem

    Hey everyone, I'm working on a program for a class that allows me to determine whether a number is perfect, deficient or abundant. I have the core of it, but I keep getting some strange errors. My professor is pretty useless when it comes to explaining things, so I was hoping someone here could help me.

    The only error (i think) is now showing up at line 40 at the public class perfectnums{ line.
    It says:
    Multiple markers at this line
    - The nested type perfectnums cannot hide an enclosing type
    - Syntax error, insert "ClassBody" to complete ClassDeclaration

    I'm confused by this... Thanks in advance!

    Java Code:
    import java.util.*;
    
    public class perfectnums {
    
    	/**
    	 * @param args
    	 */
    	//list of instance variables
    		private int sum;
    		private int num;
    		private double numPerfect;
    		private double numAbundant;
    		private double numDeficient;
    		private double total;
    		private String name;
    
    		Scanner input = new Scanner(System.in);
    		String exit = "1";
    		
    		public perfectnums(int num, String name)
    		{
    			this.num = num;
    			this.name = name;
    		}
    		
    		public int sumFactors(int num)
    		{
    			sum = 0;
    			for(int i =	1; i <= num/2; i++)
    			{
    				if(num % i ==0)
    				{
    					sum = sum + i;
    				}
    			}
    			return sum;
    		}
    		//public static void main(String[] args) {
    		//perfectnums digits = new perfectnums(num,name);
    		public class perfectnums{
    		
    		for(int i = 1; i <= num; i++)
     		{
       			System.out.print(i);
    			
       			if (i == perfectnums.sumFactors(i))
       			{
        			System.out.println("Perfect");
        			perfectnums.setNumPerfect();
       			}
       			
       			else if(i < perfectnums.sumFactors(i))
       			{
        			System.out.println("Abundant");
        			perfectnums.setNumAbundant();
       			}
    
       			else 
       			{
       				System.out.println("Deficient");
       				perfectnums.setNumDeficient();
       			}
       		
      		}
    	}
    Last edited by Eranga; 04-14-2010 at 03:48 PM. Reason: code tags added

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  3. #3
    killertofu is offline Member
    Join Date
    Apr 2010
    Posts
    2
    Rep Power
    0

    Default

    So, what would I need to do to erase the problem? I've tried making the public class perfectnums private and some other things like that, but then I get more errors.

  4. #4
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    Look at your inner class definition:
    Java Code:
    public class perfectnums{
      for(int i = 1; i <= num; i++){
      // ...
      }
    }
    no offense, but what is the for-loop doing inside a class definition and not inside some method?

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by killertofu View Post
    So, what would I need to do to erase the problem? I've tried making the public class perfectnums private and some other things like that, but then I get more errors.
    Means that you don't have an idea about the inner class. Think about that lol, without a method how can you define a simple code segment in a class?

Similar Threads

  1. Help with deficient, perfect, or abundant integer
    By saetia in forum New To Java
    Replies: 6
    Last Post: 04-07-2010, 02:52 AM
  2. help with perfect squares
    By AmplifiedKid in forum New To Java
    Replies: 1
    Last Post: 09-19-2009, 07:44 PM
  3. Ajax's confusion code problem perfect solution
    By chinafhf in forum Web Frameworks
    Replies: 0
    Last Post: 03-16-2009, 01:56 PM
  4. Way to Java Perfect
    By Javaisinmyblood in forum New To Java
    Replies: 1
    Last Post: 02-06-2008, 11:28 PM
  5. Perfect Squares
    By divyachaparala in forum New To Java
    Replies: 4
    Last Post: 02-05-2008, 09:21 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
  •