Having real trouble figuring out where i'm going wrong on this one. Building a system using WEKA in java to study associations and am trying to implement the Apriori algorithm. Currently this is the code:
package model;
import weka.associations.*;
import weka.core.Instances;
import weka.core.converters.ConverterUtils.DataSource;
public class Apriori {
public static void main(String args[]) throws Exception {
String dataset = "/Users/andrew/workspace/Movies/src/data/tagsfinal.arff";
DataSource dsource = new DataSource(dataset);
Instances dapriori = dsource.getDataSet();
Apriori apriori = new Apriori();
apriori.buildAssociations(dapriori);
System.out.println(apriori);
}
}
From looking at several implementations across the web this seems to be a widely accepted method of doing this however i receive an error on the "apriori.buildAssociations" line telling me that the method is undefined for the type Apriori. Furthermore, the import statement i use for the associations only works as the package type and when trying to extend it to :
import weka.associations.Apriori;
this throws an error message that "The import weka.associations.Apriori conflicts with a type defined in the same file". I have scoured StackOverflow alongside other resources and realize there is a lot of type undefined questions out there however have yet to find a solution to this problem. Any help/pointers would be greatly appreciated.
Your class is also named Apriori, so you are experiencing a name clash.
You should change the name of your own class to a different name (e.g. AprioriTest). In the unprobable case where you would really need your class to be named Apriori, then you would have to refer to the library's implementation by it's full name:
weka.associations.Apriori apriori = new weka.associations.Apriori();
apriori.buildAssociations(dapriori);
Related
I am coming from Java and C++ background. However, I am doing a C# application at the moment and one thing made me confused.
In Java when I import a package, it means I can access the classes that are placed in that package. For example, here I imported the ArrayList class from package java.util.
import java.util.ArrayList;
class ArrayListUtilization {
public static void main(String[] args) {
ArrayList<Integer> myList = new ArrayList<>(3);
myList.add(3);
myList.add(2);
myList.add(1);
System.out.println(myList);
}
}
Recently I had a problem in my c-sharp app, that I asked here. The funny part in my point of view is, when I added the following snippet of code:
var accountDbContext = services.GetRequiredService<AccountDbContext>();
accountDbContext.Database.EnsureCreated();
var accountDbCreator = accountDbContext.GetService<IRelationalDatabaseCreator>();
accountDbCreator.CreateTables();
I saw an error as following:
Error CS1929 'AccountDbContext' does not contain a definition for
'GetService' and the best extension method overload
'ServiceProviderServiceExtensions.GetService(IServiceProvider)'
requires a receiver of type 'IServiceProvider'
and from the error text, I understood accountDbContext object does not have GetService function. But when I press on show potential fixes, then it suggests me to add a using statement.
And it was the real fix. However, my question is what is the effect of this using statement on my object? The object is an instantiation of its class. How can adding a using statement effect on my object and add a function to it?
Note that what you are actually calling an extension method here:
accountDbContext.GetService<IRelationalDatabaseCreator>();
accountDbContext does not have a method called GetService. GetService is declared in AccessorExtensions, and the above line is just syntactic sugar for:
AccessorExtensions.GetService<IRelationalDatabaseCreator>(accountDbContext);
Now it should make sense that you need to add a using directive for the class in which the extension method is declared, in order to access the extension method.
I'm trying to use BTrace to find when a certain type is first instantiated in my program (Eclipse debugger isn't able to find it) as I'm seeing some strange behaviour (the Javolution XMLStreamWriterImpl is somehow adding elements to my XML before it should even have been created).
Anyway, I have the following method which I am using through JVisualVM, but nothing is showing up when running.
import com.sun.btrace.annotations.*;
import static com.sun.btrace.BTraceUtils.*;
import java.lang.String;
#BTrace
public class ClassLoad {
#OnMethod(clazz = "javolution.xml.stream.XMLStreamWriterImpl", method = "<init>", location = #Location(value=Kind.NEW))
public static void site(#ProbeMethodName(fqn=true) String caller) {
println(strcat("Called from #", caller));
}
}
You need a different #OnMethod definition.
#OnMethod(clazz="/.*/", method="/.*/", location=#Location(value=Kind.NEW, clazz="javolution.xml.stream.XMLStreamWriterImpl"))
Basically you specify that you want to inspect all the methods of all the classes for occurrences of new javolution.xml.stream.XMLStreamWriterImpl instructions.
The rest of the code can stay the same.
I know this is a common question asked but I've been searching and I've included the class into eclipse through the buildpath. I start to write the import statement and it autocompletes options for me so I know it's finding the class.
My problem is how come it's giving this error when I'm reading the docs and it says the constructor method is MimeUtil2() ?
http://www.jarvana.com/jarvana/view/eu/medsea/mimeutil/mime-util/2.1/mime-util-2.1-javadoc.jar!/eu/medsea/mimeutil/MimeUtil2.html#MimeUtil2()
package com.jab.app;
import java.io.File;
import eu.medsea.mimeutil.*;
public class CheckFileType {
private void GetMimeType(File filename){
MimeUtil2 test = new MimeUtil2(); //Produces the error saying java type cannot be resolved
}
I think you need to import
import eu.medsea.mimeutil.*;
According to the documentation, the type is eu.medsea.mimeutil.MimeUtil2
I ended up finding out that I was using the test-source.jar not the main jar file itself. The sourceforge page made the default as the source file instead of the main jar file.
It was buried inside of the files page.
I'm a complete beginner in Java using eclipse and even after installing those correctly external libraries,(I installed them in to my build path and they come in my referenced library section) which would make my job easy I can't use them for some reason.
import acm.*;
I used this to import all the classes of this library and when I tried to use those classes in my program, It didn't work for some reason.It gives me the following error if I try to use the method print() which is a method of the class IOconsole of this library.
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method print(String) is undefined for the type ShortPrint
at ShortPrint.main(ShortPrint.java:5)
I don't know if I missed any steps but I'm pretty sure I have installed the libraries correctly,Just can't get them to use.
EDIT 1: Heres my program.
import acm.*;
public class ShortPrint {
public static void main(String []args) {
print ("hello");
}
}
You need to have an object of ShortPrint, like so
ShortPrnt sp = new ShortPrint();
sp.print("Hello");
I am guessing you are trying to call print like this:
ShortPrint.print("Hello");
which would only work is print was a static function of ShortPrint
Another possibility is that you do not inherit ShortPrint from IOConsole, this the IOConsole.print is not accessible from ShortPrint
UPDATE: after OP added code on usage, the suggestion is to add the import
import acm.io.*;
as the IOConsole class resides in the acm.io package. Then change the call to
IOConsole cons = new IOConsole();
cons.print("hello");
as print() is not a static member of IOConsole
I believe you should change your import to:
import static acm.IOConsole.*
Since it appears that the print() method is static in IOConsole.
I have a Java library that I am working on with a directory structure which looks like the following:
/com
/example
/LibX
Server.java
Client.java
Now, from a project which is using the above classes, it seems to me that importing com.example.LibX.* and using Client client = new Client(...); is a bit ambiguous, as "Client" could mean anything. Therefore, I tried the following, only to receive "package not found" errors:
import com.example.*;
LibX.Client client = new LibX.Client(...);
It is possible to do what I described? Or is there another way to remove the ambiguity without using com.example.LibX.Client?
Java packages are not hierarchical, even if they may sometimes look like it. You can't import a "tree" of packages, as you're suggesting. You either need to import a specific package with a wildcard, or you import the specific class name, or you use fully-qualified class names in your code directly.
The following isn't ambiguous, and is considered best practice:
import com.example.LibX.Client;
...
Client client = new Client(...);
In a world where IDEs can organise your imports for you, there's no reason not to state them all explicitly.
Your concern about ambiguity is unnecessary - if you have an ambiguous reference your class won't compile -
e.g.
import java.util.Date;
import java.sql.Date;
public class Test {
private Date date;
}
won't compile. So if you can compile the class then by definition you don't have an ambiguous reference.
Incidentally LibX.Client is a bit confusing. Usually classnames are capitalized, package names lowercased, so if you did that (if LibX was a top-level package and you were giving the full name) it looks more like an inner class reference, as in Andy's response above.
It's possible if you're willing to group Client and Server as static nested classes.
public class LibX {
public static class Client {
//...
}
public static class Server {
//...
}
}
Now you can import mypak.LibX and then do new LibX.Client(). This has the unfortunate drawbacks of forcing you to group your classes and also creating the additional empty LibX class.
There's no such mechanism like what you described. Your only other possibility is to use single class imports:
import com.example.LibX.Client;