I bet you generated the getters and setters and the constructor for the initial set of fields which are these.
// private String suit;
// private String name;
// private int value;
But after changing them to
private String[] suit = { "spades", "hearts", "clubs", "diamonds" };
private String[] name = { "Ace", "Jack", "Queen", "King" };
private int[] value = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 };
you forgot to modify them accordingly. You need to change your getters and setters and constructors to something like this. The same goes with the toString()
method.
public class Card {
private String[] suit = { "spades", "hearts", "clubs", "diamonds" };
private String[] name = { "Ace", "Jack", "Queen", "King" };
private int[] value = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 };
public Card(String[] suit, String[] name, int[] value) {
super();
this.suit = suit;
this.name = name;
this.value = value;
}
public String[] getSuit() {
return suit;
}
public void setSuit(String[] suit) {
this.suit = suit;
}
public String[] getName() {
return name;
}
public void setName(String[] name) {
this.name = name;
}
public int[] getValue() {
return value;
}
public void setValue(int[] value) {
this.value = value;
}
@Override
public String toString() {
return "Card [suit=" + Arrays.toString(suit) + ", name="
+ Arrays.toString(name) + ", value=" + Arrays.toString(value)
+ "]";
}
}
Always remember to generate fresh getters, setters, constructor, toString()
methods, if you happen to change the fields in the class.
when i compile my program i receive this error:
String cannot be converted to String[]
my program is in 2 classes:
main:
import java.util.*;
import java.math.*;
public class esa{
public static void main(String[]args){
int [] numeri= new int[4];
String [] text =new String[4];
for(int i=0;i<4;i++){
text[i] = args[i];
System.out.println(text[i]);
}
text=est.conv(text);
}
}
import java.util.*;
import java.math.*;
public class est{
public static String conv(String [] valori){
}
}
asked Sep 18, 2014 at 13:54
4
text=est.conv(text);
est
Class has a method conv
which takes String[]
and returns String
.You cannot assign a String
to String[]
answered Sep 18, 2014 at 13:58
Kumar AbhinavKumar Abhinav
6,5552 gold badges23 silver badges35 bronze badges
0
error: incompatible types: String[] cannot be converted to String
I tried taking input of a 6 by 6 matrix in java using the string split function when the string is input in the following way, and to print the matrix.
1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6
The output that I get is
Main.java:24: error: incompatible types: String[] cannot be converted to String c[j] = b[i].split(" ");
my code:
import java.util.*; import java.io.*; class Solution { public static void main(String args[]) { Scanner s = new Scanner(System.in); int a[][] = new int[6][6]; String b[] = new String[6]; for (int i = 0; i < 6; i++) { b[i] = s.nextLine(); } // initializing the 2d array a[][] for (int i = 0; i < 6; i++) { for (int j = 0; j < 6; j++) { String c[] = new String[6]; c[j] = b[i].split(" "); a[i][j] = Integer.parseInt(c[j]); } } // printing the input array for (int i = 0; i < 6; i++) { for (int j = 0; j < 6; j++) { System.out.print("ta[i][j]t"); } } } }
pls, suggest how I can overcome this error
Advertisement
Answer
When we call split function of String return the String[]
. So c[j]
(which is of type String
) can’t be equal to String[]
.
Below code should be replaced as:
// initializing the 2d array a[][] for (int i = 0; i < 6; i++) { String[] c = b[i].split(" "); for (int j = 0; j < 6; j++) { a[i][j] = Integer.parseInt(c[j]); } }
- Remove From My Forums
-
Question
-
When this block of code was converted to C# from C++ it gives the error Cannot implicitly convert type ‘string[*,*]’ to ‘string[]’.The code:
public class _filter_data { public string title = new string(new char[256]); public string category = new string(new char[256]); public string copyright = new string(new char[256]); public string author = new string(new char[256]); public int[] map_enable = new int[4]; public string[] map_label = new string[4, 256]; // error occurs on these lines public int[] control_enable = new int[8]; public string[] control_label = new string[8, 256]; // error occurs on these lines public int[] control_value = new int[8]; public string[] source = new string[4, 8192]; // error occurs on these lines } The original C++ code:
typedef struct _filter_data { char title[256]; char category[256]; char copyright[256]; char author[256]; int map_enable[4]; char map_label[4][256]; int control_enable[8]; char control_label[8][256]; int control_value[8]; char source[4][8192]; }
Is there a problem with the conversion?Alternatively would there be a way to expose the struct from a managed assembly, if so what would be the managed equvilent of the C++ code.
Any help would be a
ppreciated.
-
Edited by
Monday, June 16, 2008 3:11 AM
error
-
Edited by
Answers
-
Try
public string [,] source = new ….-
Marked as answer by
jack 321
Thursday, June 19, 2008 5:36 AM
-
Marked as answer by
-
public class filter_data
{
public string title = new string(new char[256]);
public string category = new string(new char[256]);
public string copyright = new string(new char[256]);
public string author = new string(new char[256]);
public int[] map_enable = new int[4];
public string[,] map_label = new string[4, 256]; // no error now
public int[] control_enable = new int[8];
public string[,] control_label = new string[8, 256]; // no error now
public int[] control_value = new int[8];public string[,] source = new string[4, 8192]; // no error now
}
-
Marked as answer by
jack 321
Thursday, June 19, 2008 5:36 AM
-
Marked as answer by
Hi. Thanks for looking at this.
I am getting this error when trying to define and call this method.
C:UsersBillDesktoptest>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("nResultsn======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