【发布时间】:2019-04-18 00:54:05
【问题描述】:
我正在尝试比较 2 个字符串。我使用了 split 方法,然后使用了 toCharArray 方法。
毕竟我用过等于,但最后我得到:
"Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException"
import java.util.Scanner;
public class LoopsWiederholung {
public static void main (String [] args){
System.out.print("Enter the first String : ");
Scanner scan1 = new Scanner(System.in);
String s1 = scan1.next();
s1.toUpperCase();
System.out.print("Enter the second String : ");
String s2 = scan1.next();
s2.toUpperCase();
String[] s3 = new String[100];
s3 = s1.split("\\ ");
String[] s4 = new String[100];
s4 = s2.split("\\ ");
for (int i = 0 ; i< 100 ; i++){
if( s3[i].toCharArray().equals(s4[i].toCharArray())){
System.out.print(s3[i]);
}
}
}
}
【问题讨论】:
-
您将字符串与
s3[i].toCharArray().equals(s4[i].toCharArray())进行比较有什么特殊原因吗?您可以直接将它们与s3[i].equals(s4[i])进行比较。 -
@KevinAnderson 我猜是作业
-
使用 string1.equals(string2) 比较两个字符串是否相等可能会有所帮助
-
数组不认为自己与其他具有相同内容的数组相等。
-
我以为@Omar 会是个鬼
标签: java arrays exception indexoutofboundsexception