【问题标题】:Check whether two strings are similar to each other [duplicate]检查两个字符串是否彼此相似[重复]
【发布时间】:2020-10-05 18:21:50
【问题描述】:

我是java初学者。我开发了一个 Java 代码来比较两个用户输入的字符串是否包含相同频率的相似字符并将它们打印到屏幕上。但它在编译过程中显示运行时错误( ArrayOutOfBoundException )。帮我找出错误。

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;            

    }       

}

【问题讨论】:

  • 您需要考虑cd 的长度——它们可能不同。这适用于所有循环。顺便提一句。您的变量命名很糟糕 - 想出一些有意义的名称,我不知道该代码中发生了什么。
  • @Reznik 与问题无关。
  • 这个c[j] &gt;c[i+1] 是导致exception 的原因。当循环时出现i = c.length -1 条件时,它会尝试评估c[c.lenght] 并因此产生异常。

标签: java arrays string sorting runtime-error


【解决方案1】:

使用相等方法

喜欢

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" );

【讨论】:

    【解决方案2】:

    我认为您需要比较两个长度相同的字符串,它们包含的字符是否也相同?所以要做到这一点,我认为最好的选择是使用像 charAt(index) 这样的 String 类内置方法,因为这样你可以减少不必要的代码数量[创建字符数组并比较它们]上面代码的问题是你试图比较两个字符数组,但数组索引高于它已经得到的。 c[j] > c[i+1] 想想如果你到达数组 i 的最后一个索引,然后你仍然使用这个等式,你会得到那个错误,因为 lastIndex +1 不在数组中。

    所以我认为下面的代码将有助于解决您的问题。

    最佳做法:

    尝试使用带有扫描仪对象的 nextLine() 方法而不是 next() 方法,因为这样您就无法获得中间包含空格的字符串,因为 next() 只会将输入带到第一个空格。

    尝试在用户输入之前使用 System.out.println() 语句,否则用户将不知道他或她需要输入什么。

    试着想出有意义的变量名

    尝试提出 cmets,尤其是当您将代码提交给希望帮助的社区时

    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;
    }
    

    }

    或者使用 equals() 方法来比较两个字符串,但是你不必实现任何东西。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-10
      • 1970-01-01
      • 1970-01-01
      • 2010-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-08
      相关资源
      最近更新 更多