Java 检查两个字符串是否相似
我是java的初学者。我开发了一个Java代码来比较两个用户输入字符串是否包含相同频率的相似字符,并将它们打印到屏幕上。但它在编译过程中显示了一个运行时错误(ArrayOutOfBoundException)。帮我找出错误Java 检查两个字符串是否相似,java,arrays,string,sorting,runtime-error,Java,Arrays,String,Sorting,Runtime Error,我是java的初学者。我开发了一个Java代码来比较两个用户输入字符串是否包含相同频率的相似字符,并将它们打印到屏幕上。但它在编译过程中显示了一个运行时错误(ArrayOutOfBoundException)。帮我找出错误 import java.util.Scanner; public class Checker { public static void main(String[] args) { Scanner scan = new Scanner(Sy
import java.util.Scanner;
public class Checker {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String x = scan.next();
String y = scan.next();
boolean anws = similarCharctr(x, y);
System.out.println( (anws) ? "Similar" : "Dissimilar" );
}
static boolean similarCharctr(String x, String y) {
boolean status = false;
if(x.length() == y.length()){
status = false;
}
String e = x.toUpperCase();
String f = y.toUpperCase();
char c [] = new char[e.length()];
char d [] = new char[f.length()];
for(int i = 0; i<c.length; i++){
c[i] = e.charAt(i);
d[i] = f.charAt(i);
}
for(int j = 0; j< (c.length - 1); j++){
for(int i = 0; i< c.length; i++){
if( c[j] >c[i+1])
{
char temp;
temp = c[j];
c[j] = c[i+1];
c[i+1] = temp;
char temp1;
temp1 = d[j];
d[j] = d[i+1];
d[i+1] = temp1;
}
}
}
for(int i = 0; i< c.length; i++){
if(c[i] == d[i]){
status = true;
}
else{
status = false;
}
}
return status;
}
}
import java.util.Scanner;
公共类检查器{
公共静态void main(字符串[]args){
扫描仪扫描=新扫描仪(System.in);
字符串x=scan.next();
字符串y=scan.next();
布尔anws=similarCharctr(x,y);
System.out.println((anws)-“相似”:“不同”);
}
静态布尔similarCharctr(字符串x、字符串y){
布尔状态=假;
如果(x.length()==y.length()){
状态=假;
}
字符串e=x.toUpperCase();
字符串f=y.toUpperCase();
字符c[]=新字符[e.length()];
字符d[]=新字符[f.长度()];
对于(int i=0;ic[i+1])
{
焦炭温度;
温度=c[j];
c[j]=c[i+1];
c[i+1]=温度;
字符temp1;
temp1=d[j];
d[j]=d[i+1];
d[i+1]=temp1;
}
}
}
for(int i=0;i
我认为您需要比较两个长度相同的字符串,它们包含的字符是否也相同?所以要做到这一点,我认为最好的选择是使用字符串类的内置方法,比如charAt(index),因为这样可以减少不必要的代码量[创建字符数组并进行比较]上述代码的问题是,您试图比较两个chracter数组,但数组索引比已经得到的要高。c[j]>c[i+1]只要想想,如果你得到了数组i的最后一个索引,然后你仍然使用这个方程,会发生什么,你会得到那个错误,因为数组中没有最后一个索引+1
所以我认为下面的代码将有助于解决您的问题
最佳实践:
尝试对scanner对象使用nextLine()方法,而不是next(),因为这样您就无法获取包含空格的字符串,因为next()只会将输入带到第一个空格。
在用户输入之前尝试使用System.out.println()语句,否则用户将不知道他或她需要输入什么。
尝试想出有意义的变量名
试着提出意见,尤其是当你把代码放到一个需要帮助的社区时
import java.util.Scanner;
public class Checker {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Input first String ");
String str1 = scan.nextLine();
System.out.println("Input Second String ");
String str2 = scan.nextLine();
boolean validity = similarity(str1, str2) ;
System.out.println( (validity) ? "Similar" : "Dissimilar" );
}
static boolean similarity(String str1,String str2) {
if(str1.length() != str2.length()) { //Returning false because of strings length are not the same
return false;
}
for(int i = 0 ; i < str1.length() ; i++) {//comparing both strings characters at the same index
if(str1.charAt(i) != str2.charAt(i)) {
return false;
}
}
return true;
}
import java.util.Scanner;
公共类检查器{
公共静态void main(字符串[]args){
扫描仪扫描=新扫描仪(System.in);
System.out.println(“输入第一个字符串”);
字符串str1=scan.nextLine();
System.out.println(“输入第二个字符串”);
字符串str2=scan.nextLine();
布尔有效性=相似性(str1,str2);
System.out.println((有效性)?“相似”:“不同”);
}
静态布尔相似性(字符串str1、字符串str2){
如果(str1.length()!=str2.length()){//由于字符串长度不同而返回false
返回false;
}
对于(int i=0;i
}
或使用equals()方法比较两个字符串,但不必实现任何操作。使用equal方法
像
您需要考虑
c
和d
的长度-它们可能不同。这适用于所有循环。顺便说一句,你的变量命名很糟糕-请给出一些有意义的名称,我不知道代码中发生了什么。@Reznik这与问题无关。这是否回答了你的问题?这是导致异常的原因。当循环时出现i=c.length-1
条件时,将尝试计算c[c.lenght]
,从而计算异常。
Scanner scan = new Scanner(System.in);
String x = scan.next();
String y = scan.next();
boolean anws = x.equals(y);
System.out.println( (anws) ? "Similar" : "Dissimilar" );