
-----------------------------------
GDoughtUpInIt
Sat Aug 07, 2004 1:16 am

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...

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.

-----------------------------------
zylum
Sat Aug 07, 2004 6:36 pm


-----------------------------------
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...


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
Sun Aug 08, 2004 2:10 am


-----------------------------------
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
Sun Aug 08, 2004 2:28 am


-----------------------------------
here's the problem...

you're saving input into "words", but splitting up the "setence". Which doesn't exist :? 

..
String sentence;
..
sentence = in.readLine();
..
sentence.trim().split();
..


 :wink:

-----------------------------------
GDoughtUpInIt
Wed Aug 11, 2004 12:39 am


-----------------------------------
I ran this through and it just doesn't work...

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
Wed Aug 11, 2004 3:05 am


-----------------------------------
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:

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);
