Results 1 to 6 of 6
  1. #1
    jvasilj1 is offline Member
    Join Date
    Jan 2008
    Posts
    36
    Rep Power
    0

    Default Problem with code

    I am using this java code to produce output of amicable numbers...it compiles but when i run it, an error occurs in my terminal window and say "Exception in thread "main" java.lang.nosuchmethoderror: main press any key to continue...

    also i need to add a joptionpane to my code, so i can tell the program what the upper bounds is for it to search upto certain amicable numbers is.
    -----------------------------------------------------------------------
    This is my Code
    Java Code:
    import java.util.*;
    public class AmicablePairs{
    	protected static Vector<Integer> getFactors(int number){
    		Vector<Integer> result = new Vector<Integer>();
    		for (int i = 1 ; i < number; i++){
    			if (number % i == 0) result.add(new Integer(i));
    		}
    		return result;
    	}
    	protected static int sumNumbers(Vector<Integer> numbers){
    		int result = 0;
    		for (Integer integer : numbers)
    			result += integer;
    		return result;
    	}
    	protected static void printNumbers(Vector<Integer> numbers){
    		for (Integer integer : numbers)
    			System.out.println(integer);
    		System.out.println();
    	}
    
    	public AmicablePairs(int bound){
    		for (int m = 2; m <= bound; m++){
    			for (int n = 2; n <= bound; n++){
    				if (
    				    (sumNumbers(getFactors(m)) == n) &&
    				    (sumNumbers(getFactors(n)) == m))
    				{
    					System.out.println(m + ", " + n);
    				}
    			}
    		}
    	}
    }

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    "Exception in thread "main" java.lang.nosuchmethoderror
    This means that the jvm (java virtual machine) could not find a main method in the class.
    Java Code:
    	public static void main(String[] args) {
    		// Use JOptionPane.showInputDialog here
    		new AmicablePairs(25);
    	}

  3. #3
    jvasilj1 is offline Member
    Join Date
    Jan 2008
    Posts
    36
    Rep Power
    0

    Default

    where do i put that...could u add it to my code and and paste the whole thing here...

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.util.*;
    
    public class AP {
    	protected static Vector<Integer> getFactors(int number){
    		Vector<Integer> result = new Vector<Integer>();
    		for (int i = 1 ; i < number; i++){
    			if (number % i == 0) result.add(new Integer(i));
    		}
    		return result;
    	}
    	protected static int sumNumbers(Vector<Integer> numbers){
    		int result = 0;
    		for (Integer integer : numbers)
    			result += integer;
    		return result;
    	}
    	protected static void printNumbers(Vector<Integer> numbers){
    		for (Integer integer : numbers)
    			System.out.println(integer);
    		System.out.println();
    	}
    
    	public AP(int bound){
    		for (int m = 2; m <= bound; m++){
    			for (int n = 2; n <= bound; n++){
    				if (
    				    (sumNumbers(getFactors(m)) == n) &&
    				    (sumNumbers(getFactors(n)) == m))
    				{
    					System.out.println(m + ", " + n);
    				}
    			}
    		}
    	}
    
    	public static void main(String[] args) {
    		// Use JOptionPane.showInputDialog here
    		new AP(25);
    	}
    }

  5. #5
    jvasilj1 is offline Member
    Join Date
    Jan 2008
    Posts
    36
    Rep Power
    0

    Default

    Thanks a lot...My java code works now and i have jotptionpane working...I enter 10000 in the dialog box and it computes the perfect number up till 10000 and then the program ends...I need some help now in modifying my code to do that same procedure but just modify the math of it so it can compute AMICABLE NUMBERS....HERE IS MY CODE
    --------------------------------------------------------------
    Java Code:
    import javax.swing.JOptionPane;
    
    public class AmicableNumbers
    {
    public static void main(String[] args)
    {
    int upperBound, n, factor, sum;
    
    upperBound = Integer.parseInt(JOptionPane.showInputDialog
    ("Enter upper bound for perfect number search"));
    
    for(n = 2; n <= upperBound; n++)
    {
    sum = 0;
    for(factor = 1; factor <= n / 2; factor++)
    if (n &#37; factor == 0)
    sum += factor;
    
    if (n == sum)
    System.out.println(n);
    }
    }
    }
    
    // Prompt of input dialog:
    //Upper bound for amicable number search:
    
    // Sample input to input dialog:
    //10000
    
    // Output in Terminal Window:
    //6
    //28
    //496
    //8128

  6. #6
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    9

    Default

    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

Similar Threads

  1. [SOLVED] Problem with code - inheritence
    By yalla in forum New To Java
    Replies: 1
    Last Post: 03-30-2008, 07:11 AM
  2. What is the answer yo my problem with this code
    By masaka in forum New To Java
    Replies: 4
    Last Post: 03-26-2008, 07:33 AM
  3. Problem with code
    By oregon in forum New To Java
    Replies: 3
    Last Post: 08-05-2007, 06:57 PM
  4. Problem with zero in my code
    By fernando in forum New To Java
    Replies: 1
    Last Post: 08-05-2007, 07:39 AM
  5. Problem with my first code
    By paul in forum New To Java
    Replies: 2
    Last Post: 07-26-2007, 05:09 PM

Posting Permissions

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