Solved

how to use a class

Posted on 2012-03-18
7
301 Views
Last Modified: 2012-03-18
Hi. I want to use this class defined below. Would I use it by compiling it, putting it in the same directory as the program from which I call it and then call it by writing  

Arff2Database <input.arff>
 
in my program?

Do I need an import statement?
Thanks.

Usage:
 Arff2Database <input.arff>

Source code:
  import weka.core.*;
  import weka.core.converters.*;
  import java.io.*;
 
  /**
   * A simple API example of transferring an ARFF file into a MySQL table.
   * It loads the data into the database "weka_test" on the MySQL server
   * running on the same machine. Instead of using the relation name of the
   * database as the table name, "mytable" is used instead. The
   * DatabaseUtils.props file must be configured accordingly.
   *
   * Usage: Arff2Database input.arff
   *
   * @author FracPete (fracpete at waikato dot ac dot nz)
   */
  public class Arff2Database {
 
    /**
     * loads a dataset into a MySQL database
     *
     * @param args    the commandline arguments
     */
    public static void main(String[] args) throws Exception {
      Instances data = new Instances(new BufferedReader(new FileReader(args[0])));
      data.setClassIndex(data.numAttributes() - 1);
 
      DatabaseSaver save = new DatabaseSaver();
      save.setUrl("jdbc:mysql://localhost:3306/weka_test");
      save.setUser("fracpete");
      save.setPassword("fracpete");
      save.setInstances(data);
      save.setRelationForTableName(false);
      save.setTableName("mytable");
      save.connectToDatabase();
      save.writeBatch();
    }
  }
0
Comment
Question by:onyourmark
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
7 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 37735699
Yes, in general you need  class to be accessible in CLASSPATH
You need import statment if the class is in diifferent package

I think this class is designed to be called as the starting execution class, srather tah to bereferences in the ocde - it has nothing but th e main() method

so theis code needs ti be compiled into class

javac Arff2Database.java

and then used form command likene as it is written there:


java  Arff2Database input.arff
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37735716
Most of java classes represent some kind of objects, there are many way you may use them in your other c,lasees, but used most frequently and typivla it is in this way :

somewhere in your code of some other of your classe:

MyCustomClass mcc = new MyCustomClass(...possible arguments...); //create instance of the classs

mcc.methodOfClassMyCustomClass(.....parameters of the method...."); //execute method of this class
0
 

Author Comment

by:onyourmark
ID: 37735731
Hello. This code is being used to write a file to a database. I have other code (called ProcessData) and at the end of that code I want to write the results to a database and so I was going to use the class Arff2Database defined above. But Arff2Database has a main method in it so I do not know if I can call it from another class that also has a main method and I don't know if that is the proper and best way to do it. I guess I could just copy the code from the main method of Arff2Database into my ProcessData class and use it like that but I was wondering what the "java" way to do this is? I mean to have a class that does one thing (write to a database) and then call it from another class.
Thanks.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 37735741
In general you can call main method - but that is not the "java way".
"Java way" is if you want to use class form another class - you write it in such a way to use it form another class - not with main() method but with a separate say static or instance nmethod. This one is supposed to be used from the system via java command

So in this case - the simplest would be to copy the code from inside main and just use the code.
Or modify this class and put this code in method or constructor.
For such a simple case i'd rather copy the code.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37735747
actually the main work here is done by the object of the class Instances named data, so you do the same thing - use the object of this class; which still parctivcally means you'll copy the code
0
 

Author Closing Comment

by:onyourmark
ID: 37735751
Thanks. Still trying to understand the Java method. Not quite there yet though.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37735753
this is more correct statement:

actually the main work here is done by the object of the class DatabaseSaver named save, so you do the same thing - use the object of this class; which still parctivcally means you'll copy the code
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question