0

I tried to import Combinations by

import java.lang.org.apache.commons.math3.util.Combinations;

but I keep getting error when I use Combinations in my source code.

import java.util.*;
import java.org.apache.commons.math3.util.Combinations;

public class PowerSet{   //gets power set for a set containing first n integers

    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int n = Integer.parseInt(args[0]);

        for(int i=0; i<=n; i++){
            Combinations c = new Combinations(n,i);
            Iterator iter = c.iterator();
            while(iter.hasNext()){
                int[] iarr = (int[])iter.next();
                System.out.print("{" + iarr[0]);
                for(int a=1; a<iarr.length; a++){
                    System.out.println(", " + iarr[a]);
                }
                System.out.print("}, ");
            }
        }
    }
}

And the error I get clearly says the class does not exist. Am I getting the hierarchy wrong or the way I should have imported the class is wrong?

package java.org.apache.commons.math3.util does not exist
import java.org.apache.commons.math3.util.Combinations;
                                     ^
PowerSet.java:11: error: cannot find symbol
        Combinations c = new Combinations(n,i);
        ^
symbol:   class Combinations
location: class PowerSet
  • 1
    Have you added the package to your project (`Combinations` is not a Java class, but added through Apache Project library)? What IDE are you using? – AntonH Nov 17 '17 at 21:56
  • There should be no `java.` at the start of the package. You should be leaving imports to your IDE though. – bcsb1001 Nov 17 '17 at 21:58
  • 2
    Ditch the `java.lang` bit. – Joe C Nov 17 '17 at 22:00
  • @AntonH I am coding in gedit in Ubuntu at the moment. Is their something in addition to JDK that I need then? – Sanjiv Pradhanang Nov 17 '17 at 22:01
  • @bscb1001 omitting those parts didn't work either – Sanjiv Pradhanang Nov 17 '17 at 22:03
  • @Joe C didn't work – Sanjiv Pradhanang Nov 17 '17 at 22:03
  • 1
    I assume you've double-checked that Apache Commons Math is on your classpath? – Joe C Nov 17 '17 at 22:04
  • @Joe C Oh I have not. What path do I set my classpath to? – Sanjiv Pradhanang Nov 17 '17 at 22:06
  • 1
    Have you downloaded the JAR file? Have you added the JAR to your classpath? Have you changed the import to `import org.apache.commons.math3.util.Combinations;` ? – Dawood ibn Kareem Nov 17 '17 at 22:09
  • At top of your question you suggest `import <>.org.apache.commons.math3.util.Combinations;` but in your code there is `import <>.org.apache.commons.math3.util.Combinations;`. So which version did you use (correct your question accordingly)? Anyway there is no `java.lang.` nor `java.` prefix for `org.apache.commons.math3.util.Combinations`. Also `org.apache.commons.math3.util` package is not placed in standard Java, you need to download it from http://commons.apache.org/proper/commons-math/download_math.cgi and add JAR to your classpath when you are compiling and running app. – Pshemo Nov 17 '17 at 22:14
  • @DawoodibnKareem The classpath is what I am confused with. Where exactly does it go? – Sanjiv Pradhanang Nov 17 '17 at 22:15
  • [Including jar files in class path](https://stackoverflow.com/q/8084926) – Pshemo Nov 17 '17 at 22:16
  • Using an IDE (Eclipse is a very good one, and it's free) will eliminate this problem because the IDE will insert the imports for you. – chrylis -cautiouslyoptimistic- Nov 17 '17 at 22:22
  • What IDE are you using? And are you using a build tool such as Maven? Most IDEs have some kind of menu option for adding JARs to the classpath. If you're not using any IDE, then follow the link that Pshemo gave you. – Dawood ibn Kareem Nov 17 '17 at 22:23
  • Appreciate the help guys, I will try to get the jar from the link Pshemo provided and fix my classpath. I will let you guys know if it worked. – Sanjiv Pradhanang Nov 17 '17 at 22:27

1 Answers1

0

As you can see from your import statement

java.lang.org.apache.commons.math3.util.Combinations;

is not in core java / jee package rather referring a 3rd party package repository. so you need to download that package and put that into your classpath or project lib directory or use your IDE or compile command to point that package somewhere in your system. however, now a days devs are using various build tool such as maven or gradle to manage such project dependencies overheads.

Syed Ekram Uddin
  • 2,907
  • 2
  • 29
  • 33