-1

I am a beginner to java. I developed a Java code to compare whether two user input Strings contain similar characters at equal frequencies and print them to the screen. But it shows a run time error ( ArrayOutOfBoundException ) during the compilation process. Help me to find the error.

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;            

    }       

}
GDR
  • 1
  • 4
  • 2
    You need to take into account lengths of both `c` and `d` - they might be different. This applies to all loops. Btw. Your variable naming is terrible - come up with some meaningful names, I have no idea what is going on in that code. – Amongalen Jun 16 '20 at 07:52
  • 2
    @Reznik it has nothing to do with the question. – Amongalen Jun 16 '20 at 07:52
  • 2
    Does this answer your question? [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](https://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – Amongalen Jun 16 '20 at 07:54
  • this `c[j] >c[i+1]` is what is causing the `exception`. When `i = c.length -1` condition occurs while looping, this tries to evaluate `c[c.lenght]` and hence the exception. – Alanpatchi Jun 16 '20 at 07:57

2 Answers2

0

I think you need to compare two strings with the same length ,are their containing characters are also same or not ? So to do that I think the best option is to go with String class inbuilt methods like charAt(index),because then you can decrease the amount of unnecessary codes[Creation of character arrays and comparing them]Problem with your above code is you are trying to compare two chracter arrays but with a array index which is higher than it already got. c[j] > c[i+1] just think if you got to the last index of array i and then you still use this equation what happens you will get that error because lastIndex +1 is not in the array.

So I think below code will help to solve your problem.

Best practices :

Try to use nextLine() method with scanner object instead of next() because then you can't get Strings which contains spaces in between because next() will only take the input upto first space.

Try to use System.out.println() statements before the user inputs otherwise user won't know he or she needs to enter something.

Try to come up with meaningful variable names

Try to come up with comments especially when you put a code to a community expecting help

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

}

OR use equals() method to compare two Strings but then you don't have to implement anything.

Hasindu Dahanayake
  • 1,344
  • 2
  • 14
  • 37
0

use equal method

like

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