0
\$\begingroup\$

I have written a program in Java 8 using BlueJ. There is just one function of type byte that accepts two String values and return 1 if the first word comes before the second in alphabetical order, and return -1 if the first word comes after the second in alphabetical order, and 0 if both are equivalent strings. I would welcome any suggestions on how to make my code faster and more readable.

public class Test
{
    public static byte spaceshipAlphabetical (String str1, String str2)
    {
        int str1Length = str1.length();
        int str2Length = str2.length();
        int strLength;
        if 
        (str1Length < str2Length)
        {
            str2 = str2.substring(0, str1Length);
            strLength = str1.length();
        }
        else
        {
            str1 = str1.substring(0, str2Length);
            strLength = str2.length();
        }
        str1 = str1.toUpperCase();
        str2 = str2.toUpperCase();
        char charStr1;
        char charStr2;
        int intChar1 = 0;
        int intChar2 = 0;
        for 
        (int i = 0; i < strLength; i++)
        {
            charStr1 = str1.charAt(i);
            charStr2 = str2.charAt(i);
            intChar1 = (int) charStr1;
            intChar2 = (int) charStr2;
            if
            ((intChar1 < intChar2) || (intChar2 < intChar1) || ((intChar2 == intChar1) && (i == (strLength - 1))))
            { 
                break;
            }
        }
        if
        ((intChar1 < intChar2) || ((intChar2 == intChar1) && (str1Length < str2Length)))
        { 
            return 1;
        }
        else if
        ((intChar2 < intChar1) || ((intChar2 == intChar1) && (str2Length < str1Length)))
        {
            return -1;
        }
        else 
        {
            return 0;
        }
    }
\$\endgroup\$
2
  • 1
    \$\begingroup\$ Is there a reason, return str1.compare(str2);, won't work? If so include the cases where it won't. \$\endgroup\$ – user33306 Jan 8 '20 at 6:50
  • \$\begingroup\$ Uhh.. no. I just coded for practice. It is an assignment from school. The question goes: Write a program to lexicographically compare two string objects without using the java.lang.String.compareTo(String) function. \$\endgroup\$ – Riley Jones Jan 8 '20 at 6:56
2
\$\begingroup\$

[1] By Java convention curly brackets belong in the same line as the preceding statement/expression.

Instead of this:

if
()
{}
else
{}

for
()
{}

do this:

if () {
    ...
} else {
    ...
}

for () {
    ...
}

I suggest you read the Google Java style guide for more tips: https://google.github.io/styleguide/javaguide.html

[2] You don't have to introduce an intermediate variable for holding chars, you can directly assign the result of charAt() method to an int variable: intChar1 = (int) str1.charAt(i);

[3] You don't have to do all those substring operations to make the strings the same length. Rewrite the main for loop to do as many iterations as there are characters in the shorter string. Written more succinctly: min(length(A), length(B) iterations

[4] To lexicographically compare two strings, you have to find the first different character in both strings and handle the remaining edge cases after the main for loop. Some pseudocode:

for i = 0; i < (min(length(A), length(B)); i++
    if A[i] != B[i]
        if A[i] < B[i] return -1
        else return 1

// If you didn't return from the method at this point, there are only three options left
String A is shorter than string B - return -1
String B is shorter than string A - return 1
String A and string B are equal - return 0
\$\endgroup\$
4
  • \$\begingroup\$ Thanks a lot for the help. By the way, I just feel comfortable skipping lines for curly braces. \$\endgroup\$ – Riley Jones Jan 8 '20 at 7:48
  • \$\begingroup\$ That/s ok for now, but I would strongly urge to format your code according to the standard conventions, because it will make it easier for other programmers to read and understand your code. This is true no matter which programming language you'll end up writing. \$\endgroup\$ – Rok Novosel Jan 8 '20 at 7:52
  • 1
    \$\begingroup\$ google.com's Style Guide: Calling those Java conventions is bold. \$\endgroup\$ – greybeard Jan 8 '20 at 8:11
  • \$\begingroup\$ I didn't mean to imply that Google style guide is an official convention, just as an additional resource to read. \$\endgroup\$ – Rok Novosel Jan 8 '20 at 8:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.