Skip to main content
deleted 78 characters in body
Source Link
Riley Jones
  • 134
  • 1
  • 11
public class Test
{
    public static byte spaceshipAlphabetical (String str1, String str2)
    {
        int str1Length = str1.length();
        int str2Length = str2.length();
        int strLength;
        String allChars = " @#$%^&|*!?0123456789abcdefghijklmnopqrstuvwxyz";
        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,charStr1;
        char charStr2;
        int intChar1 = 0,0;
        int intChar2 = 0;
        for 
        (int i = 0; i < strLength; i++)
        {
            charStr1 = str1.charAt(i);
            charStr2 = str2.charAt(i);
            intChar1 = allChars.indexOf(charStr1int); charStr1;
            intChar2 = allChars.indexOf(charStr2int); 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;
        }
    }
}
public class Test
{
    public static byte spaceshipAlphabetical (String str1, String str2)
    {
        int str1Length = str1.length();
        int str2Length = str2.length();
        int strLength;
        String allChars = " @#$%^&|*!?0123456789abcdefghijklmnopqrstuvwxyz";
        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, charStr2;
        int intChar1 = 0, intChar2 = 0;
        for 
        (int i = 0; i < strLength; i++)
        {
            charStr1 = str1.charAt(i);
            charStr2 = str2.charAt(i);
            intChar1 = allChars.indexOf(charStr1);
            intChar2 = allChars.indexOf(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;
        }
    }
}
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;
        }
    }
edited tags
Source Link
Riley Jones
  • 134
  • 1
  • 11
public class Test
{
    public static byte spaceshipAlphabetical (String str1, String str2)
    {
        int str1Length = str1.length();
        int str2Length = str2.length();
        int strLength;
        String allChars = " @#$%^&|*!?0123456789abcdefghijklmnopqrstuvwxyz";
        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, charStr2;
        int intChar1 = 0, intChar2 = 0;
        for 
        (int i = 0; i < strLength; i++)
        {
            charStr1 = str1.charAt(i);
            charStr2 = str2.charAt(i);
            intChar1 = allChars.indexOf(intcharStr1) charStr1;;
            intChar2 = allChars.indexOf(intcharStr2) 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;
        }
    }
}
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, charStr2;
        int intChar1 = 0, 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;
        }
    }
}
public class Test
{
    public static byte spaceshipAlphabetical (String str1, String str2)
    {
        int str1Length = str1.length();
        int str2Length = str2.length();
        int strLength;
        String allChars = " @#$%^&|*!?0123456789abcdefghijklmnopqrstuvwxyz";
        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, charStr2;
        int intChar1 = 0, intChar2 = 0;
        for 
        (int i = 0; i < strLength; i++)
        {
            charStr1 = str1.charAt(i);
            charStr2 = str2.charAt(i);
            intChar1 = allChars.indexOf(charStr1);
            intChar2 = allChars.indexOf(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;
        }
    }
}
Source Link
Riley Jones
  • 134
  • 1
  • 11

Spaceship Operation in for Lexicographic Comparison

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, charStr2;
        int intChar1 = 0, 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;
        }
    }
}