actual argument String cannot be converted to String[]

Hi. Thanks for looking at this.
I am getting this error when trying to define and call this method.

 C:\Users\Bill\Desktop\test>javac JavaWekaJ48TestTrainPredReadWriteSQL.java
JavaWekaJ48TestTrainPredReadWriteSQL.java:135: error: method writeToDB in class JavaWekaJ48T
estTrainPredReadWriteSQL cannot be applied to given types;
        JavaWekaJ48TestTrainPredReadWriteSQL.writeToDB("C:/Program Files/Weka-3-7/data/weath
er.nominal.csv");
                                            ^
  required: String[]
  found: String
  reason: actual argument String cannot be converted to String[] by method invocation conversion
1 error
sion

1. It says I need String[] not String. Can you suggest what is wrong?
2. Also, I defined this new method, writeToDB, using public and static. Is this OK?
3. Then I called the method within the class definition using the code
JavaWekaJ48TestTrainPredReadWriteSQL.writeToDB
Is that the correct way to invoke this method?


Open in new window

import weka.core.Instance;
import weka.core.Instances;
import weka.core.converters.ArffLoader;
import weka.classifiers.bayes.NaiveBayesUpdateable;
 //next three are for writing to the database. Probably you don't need the others since you have '*'
 import weka.core.*;
  import weka.core.converters.*;
  import java.io.*;

import java.io.File;


import weka.classifiers.Evaluation;
import weka.classifiers.trees.J48;
import java.io.BufferedReader;
import java.io.FileReader;
import weka.core.converters.ConverterUtils.DataSource;
import weka.core.Instances;
import weka.experiment.InstanceQuery;

/**
* This example trains NaiveBayes incrementally on data obtained
* from the ArffLoader.
*
* @author FracPete (fracpete at waikato dot ac dot nz)
*/
public class JavaWekaJ48TestTrainPredReadWriteSQL { 


 /**
  * Expects an ARFF file as first argument (class attribute is assumed
  * to be the last attribute).
  *weather.nominalTest.arff
  * @param args        the commandline arguments
  * @throws Exception  if something goes wrong
  */
  
  public static void writeToDB(String[] args){
	
       /*BufferedReader readerTest = new BufferedReader(
         new FileReader("C:/Program Files/Weka-3-7/data/weather.nominalTest.arff"));
        Instances dataTest = new Instances(readerTest);
        readerTest.close();
		
			DataSource sourceTest = new DataSource("C:/Program Files/Weka-3-7/data/weather.nominal.csv");
 Instances dataTest = sourceTest.getDataSet();	

 
	     //Instances data = new Instances(new BufferedReader(new FileReader(args[0])));
		 */
		 DataSource source = new DataSource(args[0]);
 Instances data = source.getDataSet();	
 
      data.setClassIndex(data.numAttributes() - 1);
 
      DatabaseSaver save = new DatabaseSaver();
      save.setUrl("jdbc:mysql://mysql03.ixwebingrnet");
      save.setUser("C261605");
      save.setPassword("Wri");
      save.setInstances(data);
      save.setRelationForTableName(false);
      save.setTableName("weather");
      save.connectToDatabase();
      save.writeBatch();
	
	
	}
 public static void main(String[] args) throws Exception {



     /*   BufferedReader readerTrain = new BufferedReader(
         new FileReader("C:/Program Files/Weka-3-7/data/weather.nominal.arff"));
        Instances dataTrain = new Instances(readerTrain);
        readerTrain.close();
	
//If csv do it this way:	
		DataSource sourceTrain = new DataSource("C:/Program Files/Weka-3-7/data/weather.nominal.csv");
 Instances dataTrain = sourceTrain.getDataSet();
 
 */
 
  InstanceQuery queryTR = new InstanceQuery();
 queryTR.setUsername("C261605");
 queryTR.setPassword("Re");
 queryTR.setQuery("select * from weatherSample");
 // You can declare that your data set is sparse
 // query.setSparseData(true);
 Instances dataTrain = queryTR.retrieveInstances();
		
        /*BufferedReader readerTest = new BufferedReader(
         new FileReader("C:/Program Files/Weka-3-7/data/weather.nominalTest.arff"));
        Instances dataTest = new Instances(readerTest);
        readerTest.close();
		
			DataSource sourceTest = new DataSource("C:/Program Files/Weka-3-7/data/weather.nominal.csv");
 Instances dataTest = sourceTest.getDataSet();	
*/

  InstanceQuery queryTS = new InstanceQuery();
 queryTS.setUsername("C261605");
 queryTS.setPassword("Re");
 queryTS.setQuery("select * from weatherSample");
 // You can declare that your data set is sparse
 // query.setSparseData(true);
 Instances dataTest = queryTS.retrieveInstances();

 if (dataTrain.classIndex() == -1)
dataTrain.setClassIndex(dataTrain.numAttributes() - 1);
if (dataTest.classIndex() == -1)
dataTest.setClassIndex(dataTest.numAttributes() - 1);


        J48 cls = new J48();
        cls.buildClassifier(dataTrain);




Evaluation eval = new Evaluation(dataTest);
eval.evaluateModel(cls, dataTest);
System.out.println(eval.toSummaryString("\nResults\n======\n", false));
        



 for (int i = 0; i < dataTest.numInstances(); i++) {
   double pred = cls.classifyInstance(dataTest.instance(i));
   System.out.print("ID: " + dataTest.instance(i).value(0));
   System.out.print(", actual: " + dataTest.classAttribute().value((int) dataTest.instance(i).classValue()));
   System.out.println(", predicted: " + dataTest.classAttribute().value((int) pred));
        } 


	JavaWekaJ48TestTrainPredReadWriteSQL.writeToDB("C:/Program Files/Weka-3-7/data/weather.nominal.csv");
 }
}

Open in new window

onyourmarkAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ksivananthCommented:
change line 135 to,

JavaWekaJ48TestTrainPredReadWriteSQL.writeToDB( new String[]{ "C:/Program Files/Weka-3-7/data/weather.nominal.csv" } );
for_yanCommented:
your methods is defined as requiring the arrays of strings, and you call it supplying njjust one string as argiument:

JavaWekaJ48TestTrainPredReadWriteSQL.writeToDB("C:/Program Files/Weka-3-7/data/weather.nominal.csv");


Array odf string is not the same type as one string
for_yanCommented:
2. Also, I defined this new method, writeToDB, using public and static. Is this OK?
3. Then I called the method within the class definition using the code
JavaWekaJ48TestTrainPredReadWriteSQL.writeToDB
Is that the correct way to invoke this method?


This is correct - if you defined method as static you jaust need to call it with the class name

so this part is ocrrect
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

for_yanCommented:
In this case inside the method uou only use one straing args[0]

 DataSource source = new DataSource(args[0]);


so you should not declare the argument as array of strings
for_yanCommented:
I think this should work:

import weka.core.Instance;
import weka.core.Instances;
import weka.core.converters.ArffLoader;
import weka.classifiers.bayes.NaiveBayesUpdateable;
 //next three are for writing to the database. Probably you don't need the others since you have '*'
 import weka.core.*;
  import weka.core.converters.*;
  import java.io.*;

import java.io.File;


import weka.classifiers.Evaluation;
import weka.classifiers.trees.J48;
import java.io.BufferedReader;
import java.io.FileReader;
import weka.core.converters.ConverterUtils.DataSource;
import weka.core.Instances;
import weka.experiment.InstanceQuery;

/**
* This example trains NaiveBayes incrementally on data obtained
* from the ArffLoader.
*
* @author FracPete (fracpete at waikato dot ac dot nz)
*/
public class JavaWekaJ48TestTrainPredReadWriteSQL { 


 /**
  * Expects an ARFF file as first argument (class attribute is assumed
  * to be the last attribute).
  *weather.nominalTest.arff
  * @param args        the commandline arguments
  * @throws Exception  if something goes wrong
  */
  
  public static void writeToDB(String string){
	
       /*BufferedReader readerTest = new BufferedReader(
         new FileReader("C:/Program Files/Weka-3-7/data/weather.nominalTest.arff"));
        Instances dataTest = new Instances(readerTest);
        readerTest.close();
		
			DataSource sourceTest = new DataSource("C:/Program Files/Weka-3-7/data/weather.nominal.csv");
 Instances dataTest = sourceTest.getDataSet();	

 
	     //Instances data = new Instances(new BufferedReader(new FileReader(args[0])));
		 */
		 DataSource source = new DataSource(string);
 Instances data = source.getDataSet();	
 
      data.setClassIndex(data.numAttributes() - 1);
 
      DatabaseSaver save = new DatabaseSaver();
      save.setUrl("jdbc:mysql://mysql03.ixwebingrnet");
      save.setUser("C261605");
      save.setPassword("Wri");
      save.setInstances(data);
      save.setRelationForTableName(false);
      save.setTableName("weather");
      save.connectToDatabase();
      save.writeBatch();
	
	
	}
 public static void main(String[] args) throws Exception {



     /*   BufferedReader readerTrain = new BufferedReader(
         new FileReader("C:/Program Files/Weka-3-7/data/weather.nominal.arff"));
        Instances dataTrain = new Instances(readerTrain);
        readerTrain.close();
	
//If csv do it this way:	
		DataSource sourceTrain = new DataSource("C:/Program Files/Weka-3-7/data/weather.nominal.csv");
 Instances dataTrain = sourceTrain.getDataSet();
 
 */
 
  InstanceQuery queryTR = new InstanceQuery();
 queryTR.setUsername("C261605");
 queryTR.setPassword("Re");
 queryTR.setQuery("select * from weatherSample");
 // You can declare that your data set is sparse
 // query.setSparseData(true);
 Instances dataTrain = queryTR.retrieveInstances();
		
        /*BufferedReader readerTest = new BufferedReader(
         new FileReader("C:/Program Files/Weka-3-7/data/weather.nominalTest.arff"));
        Instances dataTest = new Instances(readerTest);
        readerTest.close();
		
			DataSource sourceTest = new DataSource("C:/Program Files/Weka-3-7/data/weather.nominal.csv");
 Instances dataTest = sourceTest.getDataSet();	
*/

  InstanceQuery queryTS = new InstanceQuery();
 queryTS.setUsername("C261605");
 queryTS.setPassword("Re");
 queryTS.setQuery("select * from weatherSample");
 // You can declare that your data set is sparse
 // query.setSparseData(true);
 Instances dataTest = queryTS.retrieveInstances();

 if (dataTrain.classIndex() == -1)
dataTrain.setClassIndex(dataTrain.numAttributes() - 1);
if (dataTest.classIndex() == -1)
dataTest.setClassIndex(dataTest.numAttributes() - 1);


        J48 cls = new J48();
        cls.buildClassifier(dataTrain);




Evaluation eval = new Evaluation(dataTest);
eval.evaluateModel(cls, dataTest);
System.out.println(eval.toSummaryString("\nResults\n======\n", false));
        



 for (int i = 0; i < dataTest.numInstances(); i++) {
   double pred = cls.classifyInstance(dataTest.instance(i));
   System.out.print("ID: " + dataTest.instance(i).value(0));
   System.out.print(", actual: " + dataTest.classAttribute().value((int) dataTest.instance(i).classValue()));
   System.out.println(", predicted: " + dataTest.classAttribute().value((int) pred));
        } 


	JavaWekaJ48TestTrainPredReadWriteSQL.writeToDB("C:/Program Files/Weka-3-7/data/weather.nominal.csv");
 }
}

Open in new window

onyourmarkAuthor Commented:
Thanks. I am getting errors about catching errors

C:\Users\Bill\Desktop\test>javac JavaWekaJ48TestTrainPredReadWriteSQL.java
JavaWekaJ48TestTrainPredReadWriteSQL.java:51: error: unreported exception Exception; must be
 caught or declared to be thrown
                 DataSource source = new DataSource(args[0]);
                                     ^
JavaWekaJ48TestTrainPredReadWriteSQL.java:52: error: unreported exception Exception; must be
 caught or declared to be thrown
 Instances data = source.getDataSet();
                                   ^
JavaWekaJ48TestTrainPredReadWriteSQL.java:56: error: unreported exception Exception; must be
 caught or declared to be thrown
      DatabaseSaver save = new DatabaseSaver();
                           ^
JavaWekaJ48TestTrainPredReadWriteSQL.java:64: error: unreported exception IOException; must
be caught or declared to be thrown
      save.writeBatch();
                     ^
4 errors
for_yanCommented:
you need to enclose these methods into try {


}catch(Exceoption ex){

}


braces
for_yanCommented:
I think this should work:

import weka.core.Instance;
import weka.core.Instances;
import weka.core.converters.ArffLoader;
import weka.classifiers.bayes.NaiveBayesUpdateable;
 //next three are for writing to the database. Probably you don't need the others since you have '*'
 import weka.core.*;
  import weka.core.converters.*;
  import java.io.*;

import java.io.File;


import weka.classifiers.Evaluation;
import weka.classifiers.trees.J48;
import java.io.BufferedReader;
import java.io.FileReader;
import weka.core.converters.ConverterUtils.DataSource;
import weka.core.Instances;
import weka.experiment.InstanceQuery;

/**
* This example trains NaiveBayes incrementally on data obtained
* from the ArffLoader.
*
* @author FracPete (fracpete at waikato dot ac dot nz)
*/
public class JavaWekaJ48TestTrainPredReadWriteSQL { 


 /**
  * Expects an ARFF file as first argument (class attribute is assumed
  * to be the last attribute).
  *weather.nominalTest.arff
  * @param args        the commandline arguments
  * @throws Exception  if something goes wrong
  */
  
  public static void writeToDB(String string){


try{
	
       /*BufferedReader readerTest = new BufferedReader(
         new FileReader("C:/Program Files/Weka-3-7/data/weather.nominalTest.arff"));
        Instances dataTest = new Instances(readerTest);
        readerTest.close();
		
			DataSource sourceTest = new DataSource("C:/Program Files/Weka-3-7/data/weather.nominal.csv");
 Instances dataTest = sourceTest.getDataSet();	

 
	     //Instances data = new Instances(new BufferedReader(new FileReader(args[0])));
		 */
		 DataSource source = new DataSource(string);
 Instances data = source.getDataSet();	
 
      data.setClassIndex(data.numAttributes() - 1);
 
      DatabaseSaver save = new DatabaseSaver();
      save.setUrl("jdbc:mysql://mysql03.ixwebingrnet");
      save.setUser("C261605");
      save.setPassword("Wri");
      save.setInstances(data);
      save.setRelationForTableName(false);
      save.setTableName("weather");
      save.connectToDatabase();
      save.writeBatch();
   }
catch(Exception ex){

ex.printStackTrace();
}	
	
	}
 public static void main(String[] args) throws Exception {



     /*   BufferedReader readerTrain = new BufferedReader(
         new FileReader("C:/Program Files/Weka-3-7/data/weather.nominal.arff"));
        Instances dataTrain = new Instances(readerTrain);
        readerTrain.close();
	
//If csv do it this way:	
		DataSource sourceTrain = new DataSource("C:/Program Files/Weka-3-7/data/weather.nominal.csv");
 Instances dataTrain = sourceTrain.getDataSet();
 
 */
 
  InstanceQuery queryTR = new InstanceQuery();
 queryTR.setUsername("C261605");
 queryTR.setPassword("Re");
 queryTR.setQuery("select * from weatherSample");
 // You can declare that your data set is sparse
 // query.setSparseData(true);
 Instances dataTrain = queryTR.retrieveInstances();
		
        /*BufferedReader readerTest = new BufferedReader(
         new FileReader("C:/Program Files/Weka-3-7/data/weather.nominalTest.arff"));
        Instances dataTest = new Instances(readerTest);
        readerTest.close();
		
			DataSource sourceTest = new DataSource("C:/Program Files/Weka-3-7/data/weather.nominal.csv");
 Instances dataTest = sourceTest.getDataSet();	
*/

  InstanceQuery queryTS = new InstanceQuery();
 queryTS.setUsername("C261605");
 queryTS.setPassword("Re");
 queryTS.setQuery("select * from weatherSample");
 // You can declare that your data set is sparse
 // query.setSparseData(true);
 Instances dataTest = queryTS.retrieveInstances();

 if (dataTrain.classIndex() == -1)
dataTrain.setClassIndex(dataTrain.numAttributes() - 1);
if (dataTest.classIndex() == -1)
dataTest.setClassIndex(dataTest.numAttributes() - 1);


        J48 cls = new J48();
        cls.buildClassifier(dataTrain);




Evaluation eval = new Evaluation(dataTest);
eval.evaluateModel(cls, dataTest);
System.out.println(eval.toSummaryString("\nResults\n======\n", false));
        



 for (int i = 0; i < dataTest.numInstances(); i++) {
   double pred = cls.classifyInstance(dataTest.instance(i));
   System.out.print("ID: " + dataTest.instance(i).value(0));
   System.out.print(", actual: " + dataTest.classAttribute().value((int) dataTest.instance(i).classValue()));
   System.out.println(", predicted: " + dataTest.classAttribute().value((int) pred));
        } 


	JavaWekaJ48TestTrainPredReadWriteSQL.writeToDB("C:/Program Files/Weka-3-7/data/weather.nominal.csv");
 }
}

Open in new window

for_yanCommented:
BUt in the code above I changed the arguments form array to string - so make sure that the place you call method and define it it is the same type
onyourmarkAuthor Commented:
I think what you sent works. Or it compiles.
for_yanCommented:
Good then.
In general I alwys try to test myself, but I don't have these weka jars so it is diuffocult to test for me
mccarlIT Business Systems Analyst / Software DeveloperCommented:
> 3. Then I called the method within the class definition ...

Also, since you are calling that method from within the same class, you don't necessarily need to prefix with the class name, so on line 135 you can just do...

writeToDB("C:/Program Files/Weka-3-7/data/weather.nominal.csv");

Open in new window


(Assuming that you have already changed writeToDB to take a String rather than String[])
onyourmarkAuthor Commented:
Thanks. Can I confirm,

What you changed finally is
public static void writeToDB(String string){

 and
 DataSource source = new DataSource(string);
 
 and you put the try catch stuff
 
 Is that all?
for_yanCommented:
Yes , that is all
for_yanCommented:
This part:

What you changed finally is
public static void writeToDB(String string){

 and
 DataSource source = new DataSource(string);
 
wwas necessary to match the type of argument of the mthod

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
onyourmarkAuthor Commented:
Thanks as always.
for_yanCommented:
these methods and constructors:

   DataSource source = new DataSource(args[0]);
                                     ^
 Instances data = source.getDataSet();
                                   ^
      DatabaseSaver save = new DatabaseSaver();
                           ^
      save.writeBatch();

in ther declarations specify that they throw Exception
that's why compiler chacks if these calls are enclosed in try /catch braces
onyourmarkAuthor Commented:
Ok. Great. So they have that specified as necessary in their declarations. Thanks!
for_yanCommented:
If they say in the declaraytion, say

public int method(...)  throws IOException

then when you call this method somewhere in your code, compiler will require that you enclose this call within try/catch brace or in your turn you would need to add "...throws IOException" in
the decalration of the enclosing methoid where you call the above methionde method
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.