Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 不兼容的数据类型错误_Java_Arrays_String - Fatal编程技术网

Java 不兼容的数据类型错误

Java 不兼容的数据类型错误,java,arrays,string,Java,Arrays,String,我正在“尝试”创建一个方法,该方法将获取我的字符串数组,并将其与从数据文件导入的应答键进行比较。每次编译时,我都会遇到这个不兼容的数据类型错误,它的意思是: 找到:java.lang.String 必需:java.lang.String[] 我不是在这样做吗? 我在这里和谷歌上都没有找到解决方案。它们似乎与我试图实现的目标无关 import java.util.*; // Allows for the input of a scanner method. import java.io.*;

我正在“尝试”创建一个方法,该方法将获取我的字符串数组,并将其与从数据文件导入的应答键进行比较。每次编译时,我都会遇到这个不兼容的数据类型错误,它的意思是:

找到:java.lang.String 必需:java.lang.String[]

我不是在这样做吗? 我在这里和谷歌上都没有找到解决方案。它们似乎与我试图实现的目标无关

import java.util.*;  // Allows for the input of a scanner method.
import java.io.*;    // Allows for the inputting and outputting of a data file.
import java.lang.*;  // Allows for the use of String Methods.
//////////////////////////////////////////////////////////////////////////////////////

public class TESTY
{
static Scanner testanswers;
static PrintWriter testresults;

public static void main(String[] args) throws IOException
{
    testanswers = new Scanner(new FileReader("TestInput.dat"));
    testresults = new PrintWriter("TestOutput.dat");

    String StudentID;
    String answers;
    // Reads first two lines first to know how many records there are.
    String answerKey = testanswers.nextLine();
    int count = Integer.parseInt(testanswers.nextLine());

    // Allocate the array for the size needed.
    String[][] answerArray = new String[count][];

    for (int i = 0; i < count; i++)
    {
        String line = testanswers.nextLine();
        answerArray[i] = line.split(" ", 2);
    }

    for(int row = 0; row < answerArray.length; row++)
    {
        for(int col = 0; col < answerArray[row].length; col++)
        {
            System.out.print(answerArray[row][col] + " ");
        }
        System.out.println();
    }   
    gradeData(answerArray, answerKey);



    testanswers.close();
    testresults.close();

}
///////////////////////////////////////////////////////////
//Method: gradeData
//Description: This method will grade testanswers showing 
//what was missed, skipped, letter grade, and percentage.
///////////////////////////////////////////////////////////
public static double gradeData(String[][] answerArray, String answerKey)
{   

    String key;
    double Points = 0;
    StringBuilder[] wrongAnswers = new StringBuilder[answerArray.length];

    for(int rowIndex = 0; rowIndex < answerArray.length; rowIndex++)
    {
        String studAnswers[][] = answerArray[rowIndex][1].replace(" ", "S");
        for(int charIndex = 0; charIndex < studAnswers[rowIndex][1].length; charIndex++)
        {
            if(studAnswers[rowIndex][1].charAt(charIndex).equals(key.charAt(charIndex)))
            {
                Points += 2;
            }
            if(!studAnswers[rowIndex][1].charAt(charIndex).equals('S'))
            {
                Points --;
            }
            else
            {
                wrongAnswers.setcharAt(charIndex, 'X');
            }
        }
    }
    return Points;


}

比如这句话,

String studAnswers[][] = answerArray[rowIndex][1].replace(" ", "S");
给出编译错误

Type mismatch: cannot convert from String to String[][]
因为

answerArray[rowIndex][1]。替换(“,”S”)返回一个
字符串

  • answerArray
    是一个2D
    String
    数组
  • answerArray[rowIndex][1]
    get 数组中作为字符串的元素
  • answerArray[rowIndex][1]。替换…
    替换其中的字符
    字符串
    带有另一个字符,最后作为另一个
    字符串
    (带有 替换的字符)
您正试图将其分配给
字符串
数组

此外,不能在原语(
int
char
…)上使用
equals
)。您需要使用
==
进行比较

String studAnswers[][] = answerArray[rowIndex][1].replace(" ", "S");
这里,
studAnswers
被声明为字符串数组,您可以用字符串初始化它。实际上,
answerArray
是字符串数组的数组,因此
answerArray[rowIndex][1]
是字符串。用S替换这个字符串中的所有空格,S返回另一个字符串


这没有意义。

更多信息会有所帮助…例如编译器错误出现在哪一行。这些错误从grade方法的第73行开始出现。这是我试图将studentAnswers[]]设置为我的应答数组的地方。我已更改为String studAnswers=answerArray[rowIndex][1]。替换(“,”S”);我已经意识到了那个错误。用S代替空白的想法是用S来填充空白答案,以便于评分。谢谢,对您的回答有很大帮助。我很感激。
String studAnswers[][] = answerArray[rowIndex][1].replace(" ", "S");