Jump to content

More Java Help Needed

GrayHD

I'm writing a program that asks a user to input two words/strings and then I have to display them in alphabetical order... I can get the strings saved in variables but I have no idea how to make them display in alphabetical order. Can someone help me out?

Link to comment
Share on other sites

Link to post
Share on other sites

I'm writing a program that asks a user to input two words/strings and then I have to display them in alphabetical order... I can get the strings saved in variables but I have no idea how to make them display in alphabetical order. Can someone help me out?

use

http://docs.oracle.com/javase/7/docs/api/java/lang/String.html

http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#compareTo(java.lang.String)

Mini-Desktop: NCASE M1 Build Log
Mini-Server: M350 Build Log

Link to comment
Share on other sites

Link to post
Share on other sites

So a string is essentially just a series of characters which are placed next to each other. So what you could do is separate the characters from the string and compare them with each other.

 

Another easy way would be to add the string to a array and use the sort method which would essentially do the same thing.

The only difference would be that if you were to compare it manually then you'd have to obtain the characters from the string and compare them for each other manually but when using a array and sort it through the sort command then most things would already be done for you such as comparing the characters of each string from the array.

 

The command would be Array.sort();

Link to comment
Share on other sites

Link to post
Share on other sites

So a string is essentially just a series of characters which are placed next to each other. So what you could do is separate the characters from the string and compare them with each other.

 

Another easy way would be to add the string to a array and use the sort method which would essentially do the same thing.

The only difference would be that if you were to compare it manually then you'd have to obtain the characters from the string and compare them for each other manually but when using a array and sort it through the sort command then most things would already be done for you such as comparing the characters of each string from the array.

 

The command would be Array.sort();

 

guess waht Array.sort(); is using ... right it is compareTo() 

 

Trying to split the String into chars or using an Array to sort two(!) Strings, certainly isn't good practice. 

Mini-Desktop: NCASE M1 Build Log
Mini-Server: M350 Build Log

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

×