I am having difficulties with this program. I have to compare the two arrays by reading the arrays from the console and after the user enters them, print a statement for whether or not they are true. I am not sure if I can use the compare function, but I have to do it with a for loop.
Here is what I have tried:
import java.util.Scanner;
@SuppressWarnings("unused")
public class TwoArrays {
@SuppressWarnings("unused")
public static void main(String[] args) {
Scanner input1 = new Scanner(System.in);
System.out.println("enter the first array");
String firstArrayAsString = input1.nextLine();
System.out.println("enter the second array");
String secondArrayAsString = input1.nextLine();
if (firstArrayAsString. length() != secondArrayAsString.length()){
System.out.println("false.arrays are not equal");
} else {
int arrayLen = firstArrayAsString.length();
char[] firstArray = firstArrayAsString.toCharArray();
char[] secondArray = secondArrayAsString.toCharArray();
int i = 0;
while (i < arrayLen && firstArray[i] == secondArray[i]); {
i++;
}
if (i == arrayLen) {
System.out.println("true.they are equal");
} else {
System.out.println("False.they are not equal");
}
}
input1.close();
}
}
i == arrayLen -1