Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Putting the User's Input in Alphabetical Order
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
GDoughtUpInIt




PostPosted: 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.
Sponsor
Sponsor
Sponsor
sponsor
zylum




PostPosted: Sat Aug 07, 2004 6:36 pm   Post subject: (No 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...

code:

String[] words =  sentence.trim().split("\W+");
Arrays.sort(words);


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.
GDoughtUpInIt




PostPosted: Sun Aug 08, 2004 2:10 am   Post subject: (No subject)

code:
String words;
in = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Press Enter Any 2 Words.");
words = in.readLine();
String[] words = sentence.trim().split(\W+);
Arrays.sort(words);
System.out.println(words);


I've got that. But it doesn't seem to work. *stumped*
Tony




PostPosted: Sun Aug 08, 2004 2:28 am   Post subject: (No subject)

here's the problem...

you're saving input into "words", but splitting up the "setence". Which doesn't exist Confused
code:

..
String sentence;
..
sentence = in.readLine();
..
sentence.trim().split();
..


Wink
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
GDoughtUpInIt




PostPosted: Wed Aug 11, 2004 12:39 am   Post subject: (No subject)

I ran this through and it just doesn't work...

code:
String sentence;
in = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Press Enter Any 2 Words.");
sentence = in.readLine();
sentence.trim().split();
Arrays.sort(sentence);
System.out.println(sentence);
        }       
}


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]
wtd




PostPosted: Wed Aug 11, 2004 3:05 am   Post subject: (No subject)

code:
BufferedReader in = new BufferedReader(new
   InputStreamReader(System.in));
System.out.println("Press Enter Any 2 Words.");
String sentence = in.readLine();
String[] words sentence.trim().split();
Arrays.sort(words);
for (Iterator i = words.iterator(); i.hasNext(); )
   System.out.println(i.next());


Or Java 1.5 code:

code:
BufferedReader in = new BufferedReader(new
   InputStreamReader(System.in));
System.out.println("Press Enter Any 2 Words.");
String sentence = in.readLine();
String[] words sentence.trim().split();
Arrays.sort(words);
for (String s : words)
   System.out.println(s);
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: