user12145076
user12145076

Reputation:

character array comparison ignoring case

I am working on a project for my Intro to Java class that requires me to compare two characters from two arrays and see if they are the same, ignoring the case of the letter. It compares an answer key of correct answers to a student's test answers. If the student's answer does not match the answer key answer, it should add to the number of incorrect questions.

This is the code I have so far:

   public static int gradeExam(char[] correct, char[] student)
   {
      int incorrect = 0;

      for (int i = 0; i < correct.length; i++)
      {
         if (!student[i].equalsIgnoreCase(correct[i]))
         {
            incorrect++;
         }
      }
      return incorrect;
   }

This is the answer key array:

      char[] correct = {'A', 'A', 'A', 'A', 'A', 'B', 'B', 'B', 'B', 'B',
                        'C', 'C', 'C', 'C', 'C', 'D', 'D', 'D', 'D', 'D'};

When I attempt to compile, I get this error:

DriverTest.java:53: error: char cannot be dereferenced
         if (!student[i].equalsIgnoreCase(correct[i]))
                        ^
DriverTest.java:68: error: char cannot be dereferenced
         if(!student[i].equalsIgnoreCase(correct[i]))
                       ^
2 errors

I am not sure how else to do this while ignoring the case. I am very new to Java so I apologize if this is an obvious fix. Please help, thank you!

Upvotes: 1

Views: 1438

Answers (6)

Venkatesh Nandigama
Venkatesh Nandigama

Reputation: 518

Covert both characters in to same case and compare

Character.toLowerCase(student[i]) == Character.toLowerCase(correct[i])

will do.

Upvotes: 0

R.Wedisa
R.Wedisa

Reputation: 107

The type char is a primitive -- not an object -- so it cannot be dereferenced You can convert your char to lowercase at both sides:

     if (Character.toLowerCase(student[i])!=(Character.toLowerCase(correct[i])))
     {
        incorrect++;
     }

Character class is in java.lang package.

Upvotes: 0

bathudaide
bathudaide

Reputation: 747

Because char is primitive type so it don't have method same as .methodName(), other way equalsIgnoreCase() belong to String class. Please change to Character (class that wrapper the char)

if (Character.toLowerCase(student[i]) != Character.toLowerCase(correct[i])) {
    incorrect++;
}

Upvotes: 0

Jack Wu
Jack Wu

Reputation: 11

  1. equalsIgnoreCase() is a method of String class, but student[i] is a char not a String.
  2. You may compare chars with (==, >, <, != ...)
  3. Make char[] student to uppercase first and then compare.

Upvotes: 0

Sohail Ashraf
Sohail Ashraf

Reputation: 10604

Please change both characters to lowercase with Character.toLowerCase() and then do the comparison.

if (Character.toLowerCase(student[i]) != Character.toLowerCase(correct[i]))
 {
    incorrect++;
 }

Upvotes: 0

Eran
Eran

Reputation: 393936

equalsIgnoreCase is a String method. You can't apply it on chars.

You can write

if (!Character.toLowerCase(student[i]) == Character.toLowerCase(correct[i]))

or

if (!Character.toUpperCase(student[i]) == Character.toUpperCase(correct[i]))

Upvotes: 2

Related Questions