Search code examples
javaweka

Type mismatch: cannot convert from J48 to Classifier


I am a newcomer to Weka. And I want to use Weka self-training model. I have imported weka.jar when I created the project. But I want to know how to solve this problem? Thank you in advance for you help.

enter image description here

import java.io.File;

import weka.classifiers.Classifier;
import weka.classifiers.trees.J48;
import weka.core.Instances;
import weka.core.converters.ArffLoader;

public class J48 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Classifier m_classifier = new J48();

Solution

  • You've created a new class named J48, which doesn't inherit any base class or implements any interface, so the error message is correct:

    Cannot convert from J48 to Classifier

    You probably wanted to instantiate Weka's J48 classifier. You can do that by using its fully qualified name:

    Classifier m_classifier = new weka.classifiers.trees.J48();
    

    (Also see: Java: import statement vs fully qualified name?)

    But you should generally avoid these name conflicts and rename your class to something different, e.g.:

    public class J48Demo {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Classifier m_classifier = new J48();
    

    You've already imported the correct package, so by resolving the name conflict by renaming your class new J48() will refer to the correct class.