Write a method that takes two string arrays as input parameters and returns a bo
ID: 3536056 • Letter: W
Question
Write a method that takes two string arrays as input parameters and returns a boolean value indicating if the strings in corresponding locaitons of the two arrays are identical or not.
Ok so I have been working on this problem and I thought I had got it until I ran the program and it doesn't do anything. Can anyone please tell me what I am doing wrong?
public class StringArrays {
public static void main(String [] args) {
String[] a = {"Gold", "Blue", "Pink", "Green", "Orange"};
String[] b = {"Red", "Yellow", "Pink", "Blue", "Orange"};
identical(a, b);
}
public static boolean identical(String[] c, String[] d) {
for (int i = 0; i < c.length; i++) {
if (c[i].equals(d[i]))
return true;
}
return false;
}
}
Explanation / Answer
please rate - thanks you had 2 problems-never output anything, and returned true as soon as you got a match. I fixed the code, and added another array and comparison, to show 1 where it's true, and 1 where it's false public class StringArrays { public static void main(String [] args) { String[] a = {"Gold", "Blue", "Pink", "Green", "Orange"}; String[] b = {"Red", "Yellow", "Pink", "Blue", "Orange"}; String[] c = {"Red", "Yellow", "Pink", "Blue", "Orange"}; System.out.println(identical(a, b)); System.out.println(identical(c, b)); } public static boolean identical(String[] c, String[] d) { for (int i = 0; iRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.