Sykix
Sykix

Reputation: 15

Incompatible Data Types Error

I am "attempting" to make a method that will take my string array and compare it to an answer key that I have imported from a data file. Every time I compile I get this incompatible data type error and it is saying that:

Found: java.lang.String Required: java.lang.String[][]

Am I not doing that? I have had no luck searching for a solution on here and on Google. They seem to be irrelevant to what I am trying to accomplish.

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;


}

To get a better idea of what I am doing here is my .dat file:

TFFTFFTTTTFFTFTFTFTT
5
ABC54301 TFTFTFTT TFTFTFFTTFT
SJU12874 TTTFFTFFFTFTFFTTFTTF
KSPWFG47 FT  FT  FTFTFFTTFFTF
PAR38501 FFFFFFFFFFFFFFFFFFFF
MCY19507 TTTT TTTT TTTT TT TT

Upvotes: 0

Views: 2026

Answers (2)

JB Nizet
JB Nizet

Reputation: 691625

String studAnswers[][] = answerArray[rowIndex][1].replace(" ", "S");

Here, studAnswers is declared as an array of arrays of Strings, and you initialize it with a String. Indeed, answerArray is an array of arrays of Strings, and answerArray[rowIndex][1] is thus a String. And you replace every white space in this String by an S, which returns another String.

That doesn't make sense.

Upvotes: 0

Nivas
Nivas

Reputation: 18344

This statement, for instance,

String studAnswers[][] = answerArray[rowIndex][1].replace(" ", "S");

gives the compilation error

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

because

answerArray[rowIndex][1].replace(" ", "S"); returns a String.

  • answerArray is a 2D String array.
  • answerArray[rowIndex][1] gets a element from the array which is a string
  • answerArray[rowIndex][1].replace... replaces a character in that
    String with another character, ending up as another String (with the replaced character)

You are trying to assign it to a String array.

Also, you cannot use equals on primitives (int, char...). You need to use == for comparison.

Upvotes: 1

Related Questions