Java 字符串[]无法转换为状态[]

Java 字符串[]无法转换为状态[],java,Java,只是想知道我做错了什么,我在方法setLine()中得到了一个错误,它是: 错误:不兼容的类型:字符串[]无法转换为状态[] 我不太确定如何修复它,因为我需要将该行拆分并存储在该状态数组中,以便在从csv文件读取时确定它是否为状态或位置 public static void readFile(String inFilename) { FileInputStream fileStrm = null; InputStreamReader rdr; BufferedReader

只是想知道我做错了什么,我在方法
setLine()
中得到了一个错误,它是:

错误:不兼容的类型:字符串[]无法转换为状态[]

我不太确定如何修复它,因为我需要将该行拆分并存储在该状态数组中,以便在从
csv
文件读取时确定它是否为状态或位置

public static void readFile(String inFilename)
{
    FileInputStream fileStrm = null;
    InputStreamReader rdr;
    BufferedReader bufRdr;
    int stateCount = 0, locationCount = 0;
    String line;

    try
    {
        fileStrm = new FileInputStream(inFilename);
        rdr = new InputStreamReader(fileStrm);
        bufRdr = new BufferedReader(rdr);
        line = bufRdr.readLine();
        while (line != null)
        {
            if (line.startsWith("STATE"))
            {
                stateCount++;
            }
            else if (line.startsWith("LOCATION"))
            {
                locationCount++;
            }
            line = bufRdr.readLine();
        }
        fileStrm.close();

        State[] state = new State[stateCount];
        Location[] location = new Location[locationCount];
    }
    catch (IOException e)
    {
        if (fileStrm != null)
        {
            try { fileStrm.close(); } catch (IOException ex2) { }
        }
        System.out.println("Error in file processing: " + e.getMessage());
    }
}

public static void processLine(String csvRow)
    {
        String thisToken = null;
        StringTokenizer strTok;
        strTok = new StringTokenizer(csvRow, ":");
        while (strTok.hasMoreTokens())
        {
            thisToken = strTok.nextToken();
            System.out.print(thisToken + " ");
        }
        System.out.println("");
    }

public static void setLine(State[] state, Location[] location, int stateCount, int locationCount, String line)
{
    int i;
    state = new State[stateCount];
    state = line.split("="); <--- ERROR
    for( i = 0; i < stateCount; i++)
    {
    }
}

public static void writeOneRow(String inFilename)
{
    FileOutputStream fileStrm = null;
    PrintWriter pw;
    try
    {
        fileStrm = new FileOutputStream(inFilename);
        pw = new PrintWriter(fileStrm);
        pw.println();
        pw.close();
    }
    catch (IOException e)
    {
        if (fileStrm != null)
        {
            try
            {
                fileStrm.close();
            }
            catch (IOException ex2)
            {}
        }
        System.out.println("Error in writing to file: " + e.getMessage());
    }

}
publicstaticvoidreadfile(字符串填充名)
{
FileInputStream fileStrm=null;
输入流阅读器rdr;
BufferedReader bufRdr;
int stateCount=0,locationCount=0;
弦线;
尝试
{
fileStrm=新文件输入流(inFilename);
rdr=新的InputStreamReader(fileStrm);
bufRdr=新的缓冲读取器(rdr);
line=bufRdr.readLine();
while(行!=null)
{
if(第行开始与(“状态”))
{
stateCount++;
}
else if(第行起始位置)(“位置”))
{
locationCount++;
}
line=bufRdr.readLine();
}
fileStrm.close();
状态[]状态=新状态[状态计数];
位置[]位置=新位置[locationCount];
}
捕获(IOE异常)
{
如果(fileStrm!=null)
{
请尝试{fileStrm.close();}捕获(IOException ex2){}
}
System.out.println(“文件处理错误:+e.getMessage());
}
}
公共静态无效处理行(字符串csvRow)
{
字符串thisToken=null;
StringTokenizer-strTok;
strTok=新的StringTokenizer(csvRow,“:”);
while(strTok.hasMoreTokens())
{
thisToken=strTok.nextToken();
系统输出打印(thisToken+“”);
}
System.out.println(“”);
}
公共静态void setLine(State[]State,Location[]Location,int stateCount,int locationCount,字符串行)
{
int i;
状态=新状态[状态计数];

state=line.split(“=”)发生此错误,因为它只是说“
String[]
无法转换为
state[]
”。这就像您想将
整数存储到
字符串中一样,因为类型之间没有关系(父->子)

因此,如果您想解决问题,您需要一种将
字符串[]
转换为
状态[]
的方法

private State[] toStateArray(String[] strings){
    final State[] states = new State[strings.length];
    for(int i = strings.length-1; i >= 0; i--){
        states[i] = new State(strings[i]); // here you have to decide how to convert String to State
    }
    return states;
}

发生此错误时,它只是说“
字符串[]
无法转换为
状态[]
”。这就像您想将
整数
存储到
字符串
,这是相同的,因为类型之间没有关系(父->子)

因此,如果您想解决问题,您需要一种将
字符串[]
转换为
状态[]
的方法

private State[] toStateArray(String[] strings){
    final State[] states = new State[strings.length];
    for(int i = strings.length-1; i >= 0; i--){
        states[i] = new State(strings[i]); // here you have to decide how to convert String to State
    }
    return states;
}

state=line.split(“=”
之所以会出现错误,是因为
state
的类型为
state[]
,而
line.split(“=”
返回一个
字符串[]
。您不能在任意类型之间自由分配。您必须迭代生成的字符串数组,并使用元素自己解析
状态
对象。您能为类
状态
@Karan提供代码吗?为什么?您认为
状态
会继承
字符串
字符串
最终的
太不可能了。@AxelH它确实有意义。也许这个类已经有了一个接受字符串的构造函数?@QBrute我明白了,但是解释这个问题就足以让OP适应了。这是一个基本问题,意味着他需要理解它,而不仅仅是粘贴一个解决方案。
state=line.split(=”)
之所以会出现错误,是因为
状态
的类型为
状态[]
,而
行。split(“=”)
返回一个
字符串[]
。您不能在任意类型之间自由分配。您必须迭代生成的字符串数组,并使用元素自己解析
状态
对象。您能为类
状态
@Karan提供代码吗?为什么?您认为
状态
会继承
字符串
字符串
最终的
太不可能了。@AxelH它确实有意义。也许这个类已经有了一个接受字符串的构造函数?@QBrute我明白了,但是解释这个问题就足以让OP适应了。这是一个基本问题,意味着他需要理解它,而不仅仅是粘贴一个解决方案。谢谢你的回复和帮助。我尝试将它添加到我的代码中,现在我开始在for循环中设置错误行:类状态中的构造函数状态不能应用于给定的类型;对于代码行:
states[i]=新状态(strings[i]);
是的,这是因为此构造函数(
State(String)
)不存在。现在由您决定需要使用什么构造函数,但我的示例应该告诉您如何使用;)感谢您的回答和帮助。我尝试将其添加到代码中,现在我在for循环中得到了错误行:类状态中的构造函数状态不能应用于给定类型;对于代码行:
状态[I]=newstate(strings[i]);
是的,这是因为这个构造函数(
State(String)
)不存在。现在由您决定需要使用什么构造函数,但我的示例应该告诉您如何使用;)