1

I'm developing a dictation program in which I try to compare text entered by a student to the correct text but I have errors on compiling which doesn't make sense to me.

CODE:

String[] list_answer = exercise_english_1.answer.split("\\s+");
String[] list_responce = exercise_english_1.response.split("\\s+");
ArrayList<String> list_error = new ArrayList<String>();

for(int i = 0; i < list_answer.length(); ++i) {
    if (list_responce[i].equals(list_answer[i]))
        exercise_english_1_statistics.score += (double) 1 / list_answer.length();
    else
        list_error.add(list_responce[i]);
}

ERRORS:

C:\Users\?????\OneDrive\Java\Project\Prototype\Prototype.java:71: error: cannot find symbol
                        for(int i = 0; i < list_answer.length(); ++i) {
                                                      ^
  symbol:   method length()
  location: variable list_answer of type String[]
C:\Users\?????\OneDrive\Java\Project\Prototype\Prototype.java:73: error: cannot find symbol
                                        exercise_english_1_statistics.score += (double) 1 / list_answer.length();

  symbol:   method length()
  location: variable list_answer of type String[]
C:\Users\?????\OneDrive\Java\Project\Prototype\Prototype.java:208: error: cannot find symbol
                        for(int i = 0; i < list_answer.length(); ++i) {
                                                      ^
  symbol:   method length()
  location: variable list_answer of type String[]
C:\Users\?????\OneDrive\Java\Project\Prototype\Prototype.java:210: error: cannot find symbol
                                        exercise_french_1_statistics.score += (double) 1 / list_answer.length();
                                                                                                      ^
  symbol:   method length()
  location: variable list_answer of type String[]
4 errors

PS: Sorry for my bad English, it's not my native language...

Mureinik
  • 297,002
  • 52
  • 306
  • 350
Rémi
  • 75
  • 1
  • 1
  • 9
  • 3
    Arrays have `length` field, not `length()` method. – Pshemo Jun 11 '16 at 20:57
  • Also read: [length and length() in java](http://stackoverflow.com/questions/1965500/length-and-length-in-java) – Pshemo Jun 11 '16 at 20:59
  • But I have parenthesis on other similar arrays in my code and the compile and execute just fine. – Rémi Jun 11 '16 at 23:09
  • I am guessing that either that code is not Java or you didn't use it on array but on String object like `stringArray[0].length()` (assuming that `stringArray[0]` returns `String` instance). – Pshemo Jun 11 '16 at 23:14

1 Answers1

3

Arrays don't have a length() method, they have a length public member:

for(int i = 0; i < list_answer.length; ++i) {
    // Here (note: no ()) -----^
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • Man, this is confusing! First I had to remember that Java had `length()` and JavaScript had `length`; and now even Java has `length`? – cst1992 Jun 21 '22 at 07:29