Computer Science Canada Putting the User's Input in Alphabetical Order |
Author: | GDoughtUpInIt [ Sat Aug 07, 2004 1:16 am ] |
Post subject: | Putting the User's Input in Alphabetical Order |
Hey, how is everybody? Just having a small problem figuring out how to take the user's input and then output in alphabetical order. Here's what I have... Quote: String twoWords;
in = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Press Enter Any 2 Words."); twoWords = in.readLine(); I also need to display how many letters are in each word. I guess you would have to count the total amount of characters, and then count spaces, and then subtract the spaces from total characters. Anyways, I'm still in the learning process, indeed. If someone knows the code, and can help me out, I'd really appreciate it. Thanks. -Justin. |
Author: | zylum [ Sat Aug 07, 2004 6:36 pm ] | ||
Post subject: | |||
after you get your sentence string, use the split method with a regular expresion to divide your sentence into words... then use the Array.sort method to sort it into alphabetical order...
notice i trimed the sentence before spilting it to remove any blank spaces before or after the sentence... otherwise you might get empty strings in you array. |
Author: | GDoughtUpInIt [ Sun Aug 08, 2004 2:10 am ] | ||
Post subject: | |||
I've got that. But it doesn't seem to work. *stumped* |
Author: | Tony [ Sun Aug 08, 2004 2:28 am ] | ||
Post subject: | |||
here's the problem... you're saving input into "words", but splitting up the "setence". Which doesn't exist
|
Author: | GDoughtUpInIt [ Wed Aug 11, 2004 12:39 am ] | ||
Post subject: | |||
I ran this through and it just doesn't work...
It comes back with 2 errors, both saying "cannot resolve symbol". Would somebody be able to test it out and give me some feedback? Would be much appreciated. Thanks, George.[/code] |
Author: | wtd [ Wed Aug 11, 2004 3:05 am ] | ||||
Post subject: | |||||
Or Java 1.5 code:
|