Home > database >  Comparing two char arrays in Java
Comparing two char arrays in Java

Time:02-20

I am trying to compare two char arrays lexicographically, using loops and arrays only. I solved the task, however, I think my code is bulky and unnecessarily long and would like an advice on how to optimize it. I am a beginner. See code below:

    //Compare Character Arrays Lexicographically
    //Write a program that compares two char arrays lexicographically (letter by letter).
    // Research how to convert string to char array.

    Scanner scanner = new Scanner(System.in);
    String word1 = scanner.nextLine();
    String word2 = scanner.nextLine();

    char[] firstArray = word1.toCharArray();
    char[] secondArray = word2.toCharArray();

    for (char element : firstArray) {
        System.out.print(element   " ");
    }
    System.out.println();
    for (char element : secondArray) {
        System.out.print(element   " ");
    }
    System.out.println();

    String s = String.valueOf(firstArray);
    String b = String.valueOf(secondArray);

    int result = s.compareTo(b);
    if (result < 0) {
        System.out.println("First");
    } else if (result > 0) {
        System.out.println("Second");
    } else {
        System.out.println("Equal");
    }

}

}

CodePudding user response:

I think its pretty normal. You've done it right. There's not much code to reduce here , best you can do is not write the two for loops to print the char arrays. Or if you are wanting to print the two arrays then maybe use System.out.println(Arrays.toString(array_name)); instead of two full dedicated for/for each loops. It does the same thing in the background but makes your code look a little bit cleaner and that's what you are looking for.

CodePudding user response:

As commented by tgdavies, you schoolwork assignment was likely intended for you to compare characters in your own code rather than call String#compareTo.

In real life, sorting words alphabetically is quite complex because of various cultural norms across various languages and dialects. For real work, we rely on collation tools rather than write our own sorting code. For example, an e with the diacritical may sort before or after an e without, depending on the cultural context.

But for a schoolwork assignment, the goal of the exercise is likely to have you compare each letter of each word by examining its code point number, the number assigned to identify each character defined in Unicode. These code point numbers are assigned by Unicode in roughly alphabetical order. This code point number ordering is not sufficient to do sorting in real work, but is presumably good enough for your assignment, especially for text using only basic American English using letters a-z/A-Z.

So, if the numbers are the same, move to the next character in each word. When you reach the nth letter that are not the same in both, then in overly simplistic terms, you know which comes after which alphabetically. If all the numbers are the same, the words are the same.

Another real world problem is the char type has been legacy since Java 5, essentially broken since Java 2. As a 16-bit value, char is physically incapable of representing most characters.

So instead of char arrays, use int arrays to hold code point integer numbers.

int[] firstWordCodePoints = firstWord.codePoints().toArray() ;
  • Related