+ 1

Why does this code in java not work?

Why does this code in java not work? public static boolean compare(String a, String b) { for (int i = 0; i < a.length(); i++) if (a[i] == b[i]) return true; return false; } Error: Unresolved compilation problems: The type of the expression must be an array type but it resolved to String. This code I've got from Java Challenge wrote by Norayr Gasparyan

29th Sep 2018, 8:44 PM
vvv
vvv - avatar
1 Answer
+ 2
I'm not a Java Expert, but apparently you have to use charAt to access a character in a String. So your comparison should read like: if(a.charAt(i)==b.charAt(i)) Ref: https://stackoverflow.com/questions/21110379/must-be-an-array-type-but-resolved-to-string
30th Sep 2018, 5:43 AM
fra
fra - avatar