
-----------------------------------
magic
Sat Dec 25, 2010 6:02 am

Java
-----------------------------------
please help me with this problem..  :)
It's my first time to learn about Java..


Write a program that reads in a whole bunch of words (each word is no more than 20 characters) and spit out the duplicate words. For our purposes, words are to be separated by spaces, commas (,), !, ?, and periods (.). No word goes past the end of a line. All comparisons should ignore case.
Output: Output the sorted words, one per line, in lower case. Display same words only once.
Sample Input:
How much wood would a woodchuck chuck if a woodchuck could chuck wood? Well? What
do you think? Are you feeling well?
Sample Console:


a
are
chuck
could
do
feeling
how
if
much
think
well
what
wood
woodchuck
would


-----------------------------------
ProgrammingFun
Sat Dec 25, 2010 9:31 am

RE:Java
-----------------------------------
Which part do you need help with? We cannot give you the complete code but can help you with concepts, debugging etc...

-----------------------------------
magic
Tue Dec 28, 2010 9:27 am

RE:Java
-----------------------------------
thank you very much..

I'm just confused on how can I spit out the words and display the same words only once?

-----------------------------------
ProgrammingFun
Tue Dec 28, 2010 11:33 am

Re: Java
-----------------------------------
You can use the split method to split all of the words into an array:

String test = "This is my string."; //This is the string you are using
String

After this, you can use a declare a new array which is the size of the array containing the words.
Then initialize a for loop which reads thru the current array, stores the word being processed, and then if it is being repeated and is not already been stored in the second array, you should store it in the second array.

I hope that makes some sense...there might be more efficient ways to do this but this is what came to mind first.  :wink:

-----------------------------------
jcollins1991
Tue Dec 28, 2010 12:44 pm

Re: Java
-----------------------------------
edit: Actually, since it's your first time learning Java you should probably first read up on stuff like for loops and strings. After you understand those read up on a few sorting algorithms http://en.wikipedia.org/wiki/Sorting_algorithm.

Except there's a list of characters to split on, while the string split function only takes one. Iterating through the array would probably be easiest, creating a new word every time you see one of the characters that the OP mentioned. Then make the entire array of words lower case, sort the array of all the words, and then iterate over the sorted array and find all the unique words (you can figure that part out yourself :P).

Depending on what sort you use this method can be faster than the one that ProgrammingFun gave (using merge sort you should end up with O(nlog(n)) instead of O(n^2) AKA faster as your input grows), but if you're going to do something like bubble sort just use the method ProgrammingFun gave (but don't forget about case-insensitivity and sorting it at the end).

-----------------------------------
magic
Wed Dec 29, 2010 6:37 am

RE:Java
-----------------------------------
Thank you so much for the information..

I tried to make one but unfortunately this code is still lacking..
-All comparisons should ignore case and duplicate words..
-because I used for loop, the words to be printed are not enough..

import java.util.Scanner;
public class Main
{
	public static void main(String[] args)
	{
		String[] Array;
		Array=new String[10];
		String temp;

		Scanner input=new Scanner(System.in);
System.out.println("Enter Sentences:");
		for(int i=0;i