The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

Help Please - Rewriting text

Hi everyone,
I hope someone can help, I need to read a text file that the user inputs, the program then has to find words that appear more than 3 or 4 times and replace them with a shorthand version.
so the following text:

"hello, some times some people say hello and some times they don't, times sure are changing, say hello to the future"

My idea was to take the input file, and add each string to an array. Then re-read the file, and check each word to see if it appears more than twice in the array. If it does then output is substring of the array concatenated with an asterix, otherwise just output the string as it appears. . . problem is I can't seem to implement my plan very effectively at all

Re: Help Please - Rewriting text

Hi Kevin,
First of all, I thought maybe I was over complicating the problem by using the method I mentioned above.
The problem I seem to be having is the second loop is used to check each word and it's corresponding number of appearances before starting to write to the file

Last edited by dechno; January 21st, 2011 at 06:15 PM.
Reason: silly code

Re: Help Please - Rewriting text

Well once you exit the first loop, you've gone through the whole file already, so in.hasNext() will return false.

I would think that you should break this up into a couple different steps- the first step is to put all of the words into some kind of data structure (an array or a List, whatever). The second step is taking a List of words and converting the duplicates to the translated code words.

Re: Help Please - Rewriting text

Originally Posted by dechno

Hi everyone,
I hope someone can help, I need to read a text file that the user inputs, the program then has to find words that appear more than 3 or 4 times and replace them with a shorthand version.
so the following text:

"hello, some times some people say hello and some times they don't, times sure are changing, say hello to the future"

My idea was to take the input file, and add each string to an array. Then re-read the file, and check each word to see if it appears more than twice in the array. If it does then output is substring of the array concatenated with an asterix, otherwise just output the string as it appears. . . problem is I can't seem to implement my plan very effectively at all

Any ideas?

The way you have it appear, it looks like the first occurrence, it's the whole word, but if again, then it's just the first character. If you can figure out how to figure which ones are more than twice, just change the 2nd and so on to that word . charAt(0) + "*".

Re: Help Please - Rewriting text

Originally Posted by javapenguin

The way you have it appear, it looks like the first occurrence, it's the whole word, but if again, then it's just the first character. If you can figure out how to figure which ones are more than twice, just change the 2nd and so on to that word . charAt(0) + "*".

Once again, most of this information is useless. You basically repeated the OP's goals and then threw out the most trivial piece of the algorithm. Why do you keep doing stuff like this?