Jump to content

Need help comparing which word comes before in the alphabet (JAVA)

CreepyPro

I need to write a method that accepts two arrays of Strings and returns true if each element in the first array comes before the element at the same index in the second array.The method should return false if the arrays are not the same length.
For example, “car” comes before “motorcycle” in the alphabet.

 

I have this so far but I can't figure out a way to compare which word comes first in the alphabet.

public static boolean allBefore(String s[], String w[]) {
        int lens = s.length;
        int lenw = w.length;
        int c = 0;
        if (lens != lenw)
            return false;
        else
            for (int i=0; i<lens; i++) {
                
            }
    }

 

Link to comment
Share on other sites

Link to post
Share on other sites

You can use the .charAt() method for your string, look at the first character (so give 0 as a parameter, to look at the first character) and do a simple lesser than greater than comparison (because I believe even in Java, you can do a simple > or < for checking what comes first in the alphabet).

Should look a bit like this:

for (int i=0; i < arrayLength; i++) 
{
     if(stringArray1[i].charAt(0) > stringArray2[i].charAt(0))
     {
     	// Point for one team? Return true or false? Depends on what you want to do.
     }
     else
     {
     	// Point for the other team?
     }
}

 

Depending on how far you want it to go, you might want to add a third clause where both first character are the same. If you for example were to compare "car" and "cat", it wouldn't trigger the if and the else was not actually valid..

But that depends on if you need to go that far with this assignment.

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

You could iterate over the strings and compare each character until they are different or one string ends. Comparing chars should work with "<" and don't forget to set both strings to lower case. I won't post code, since this seems like homework.

My boring Github   /人◕ ‿‿ ◕人\

Link to comment
Share on other sites

Link to post
Share on other sites

14 minutes ago, minibois said:

You can use the .charAt() method for your string, look at the first character (so give 0 as a parameter, to look at the first character) and do a simple lesser than greater than comparison (because I believe even in Java, you can do a simple > or < for checking what comes first in the alphabet).

Should look a bit like this:


for (int i=0; i < arrayLength; i++) 
{
     if(stringArray1[i].charAt(0) > stringArray2[i].charAt(0))
     {
     	// Point for one team? Return true or false? Depends on what you want to do.
     }
     else
     {
     	// Point for the other team?
     }
}

 

Depending on how far you want it to go, you might want to add a third clause where both first character are the same. If you for example were to compare "car" and "cat", it wouldn't trigger the if and the else was not actually valid..

But that depends on if you need to go that far with this assignment.

thanks for the help,  I couldn't remember charAt() 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×