Solved

how to use a class

Posted on 2012-03-18
7
297 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
  • 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
IT Company 5 69
strCount chalenge 3 51
Unable to start eclipse ? 17 87
sites similar to codingbat to improve coding hanson skills 3 25
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

747 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now