Ok, I've got this code, it says that there are no main classes and that public class Amicable; class Amicable is public, should be declared in a file named Amicable.java, but the file name is Amicable... Any help would greatly help me, since I have same prob. with another file>_>

public class Amicable
{
  public Amicable() {
  }
  int duplicates = 0;

  public int findAddFactors(int num)
  {
    int factorSum = 1;
    for (int i=2; i<=(num /2); i++)
    {
      if ((num%i)==0)
	factorSum+=i;
    }
    return factorSum;
  }
  public String areAmicable(int num)
  {
    if (num!=duplicates)
    {
      int pair1 = findAddFactors(num);
      int pair2 = findAddFactors(pair1);
      if ((num==pair2) && (pair1!=pair2))
      {
	duplicates = pair1;
	return "("+pair2+","+pair1+")";
      }
      else
	return null;
    }
    else
      return null;
  }

  public static void main(String args[])
  {
  Amicable test = new Amicable();
  for (int i=1; i<=100000;i++)
  {
    String res = test.areAmicable(i);
    if (res!=null)
      System.out.println(res);
  }
  }
}

Recommended Answers

All 6 Replies

The above code should be in a file called "Amicable.java". To compile, type "javac Amicable.java". This should create a file called "Amicable.class". To run it, type "java Amicable".


Is that what you are doing?

I want to run it in netbeans, to see if I get the result I want.

I want to run it in netbeans, to see if I get the result I want.

Still needs to be in a file called "Amicable.java" if it is not already. If it's in NetBeans, you can ignore the rest of my earlier post.

And how do I do that?

First time using netbeans and coding in java lang.

I right clicked on the project name and changed it to Amicable.java and the output folder, that didn't help...

And how do I do that?

First time using netbeans and coding in java lang.

I right clicked on the project name and changed it to Amicable.java and the output folder, that didn't help...

Don't change the project name. You can name the project whatever you'd like. You have a couple of options.

You can go into the src folder for the project and rename the file to Amicable.java manually there (not through NetBeans).

NetBeans may let your "refactor" the name to the correct name by right-clicking the file (though I've never tried it where it was a matter of not having the .java extension at the end).

Delete the file and create a new CLASS (not a new project) called Amicable. NetBeans will add the .java extension automatically. Then copy and paste the code into the new file.


So it's a matter of finding out what the file name is now and seeing

  1. Does it end in .java?
  2. Is the part before the dot "Amicable"?

Cool thxz, this error is gone:
class Amicable is public, should be declared in a file named Amicable.java

And it works!

(220,284)
(1184,1210)
(2620,2924)
(5020,5564)
(6232,6368)
(10744,10856)
(12285,14595)
(17296,18416)
(63020,76084)
(66928,66992)
(67095,71145)
(69615,87633)
(71145,67095)
(76084,63020)
(79750,88730)
(87633,69615)
(88730,79750)
BUILD SUCCESSFUL (total time: 25 seconds)

Now I can fix my other problems, thxz a bunch!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.