• Post Reply Bookmark Topic Watch Topic
  • New Topic

Searching a text document, printing needed strings.  RSS feed

 
Rob Rowan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay everyone, I made a thread on here earlier on ROUGHLY the same topic but not quite. I've gotten a little further so far and I REALLY need some help.
Basically, I have a randomly generated set of characters(8) ranging from A-Z.
Let's say for arguments sake I get the following characters; T C E E I S P W
I've managed to get Java to search through an english dictionary text file and return any words that contain these characters, the problem is I get duplicates of characters there isn't duplicates of.
i.e. I get words like Cesspipe(two S's even though there's only one in the generated characters. Also doesn't make use of the W or the T)
Peetweet(Uses 4 E's and doesn't use the C I or S)

I'm looking for a way to look for strings that are basically an ANAGRAM of the randomly generated character list.
So far my code looks like this


What this does is it takes the randomly generated characters and deletes them from a list of all the letters in the alphabet. Then runs that list through an if statement every time the reader reads a line. If the line contains one of the characters I don't want, it makes the line null. Then if the line isn't null, it'll print it out.

So far I'm getting prints of every word in the dictionary that is made up of only those characters but I'm getting weird duplicates as previously stated. I have NO idea how to handle this and would really appreciate a point in the right direction. Thank you for your time!
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob, please reformat your code in the original post to avoid using such long lines, so break them. They spoil the layout of the page and make it look ugly and difficult to read.
 
Rob Rowan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Will do in future, sorry about that!
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is an Edit option for your original post, so you might as well do it now.
 
Rob Rowan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am currently, on my mobile so it's taking a while. I apologise if I seemed lazy prior.
 
David Pacsuta
Greenhorn
Posts: 8
Android Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It has nothing to do with the solution but you can remove "== true" everywhere in your code. For example: is exactly the same as if you write because the condition itself is a boolean value.
 
Tony Docherty
Bartender
Posts: 3268
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It has nothing to do with the solution but you can remove "== true" everywhere in your code.

I would say you "should remove "== true" everywhere in your code". As David has correctly said it is superfluous but also explicitly testing a boolean == true or false can be a potential typo bug. If you are testing an assignable variable and accidentally use '=' instead of '==' you will assign the value rather than testing it. This is not a compilation error so as it is probably not what you intended many IDE's will warn you of this potential pitfall.

For example:

This compiles but prints "ok is true" which probably wasn't what you wanted.
 
Rob Rowan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for helping me clean up the code, it was a bit messy but I'm still really stuck with this! Any help would be appreciated.
 
Tony Docherty
Bartender
Posts: 3268
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I joined this thread late and assumed it had been answered.

I don't understand what your code is supposed to do, it doesn't appear to match your description of the problem.

For instance:
What is in the test.txt file - is it your dictionary. Why are you reading it in and storing it in a fixed size array when you can't know how many words are in the file. Also you then don't use the array anywhere.
Why are you putting every letter in 'list'?
What is 'FinalArray'?
Why are you testing the read in line contains the first 18 letters in 'list', what about the other letters and why not do this is a loop. (If you ever find yourself writing the same code again and again with just an index that changes you need a loop)

Have I totally misunderstood what you are trying to achieve?
Have you written out how to solve this in words as if you were doing it yourself with a pen and paper?
 
Rob Rowan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I have and the problem lies with the duplicates.
Final array is the array where I store the 8 random characters generates by a previous bit of code. Test.txt is the dictionary and I'm storing It I'm an array in case I need to use it. I'm checking if the read line contains those 18 because I removed 8 out of 26 of the alphabet(the 8 random letters) if I search for just the 8 ransoms I get words that contain those letters but also other letters.
What I'm trying to do is if I get these 8 characters:
T O O F A B L L
And search through the dictionary ill get football.
And not something that contains multiple Bs As or Fs
 
Tony Docherty
Bartender
Posts: 3268
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you saying the way you would do this yourself is to test every letter you don't want to see if it is in the word. I would have thought most people would check to see if the letters they did want were in the word and, to handle duplicates, would cross out each letter they found.

Ok, read the dictionary into a list rather than an array, that way if you add more words to your text file you won't need to change your code. And if you want to read all the words in for later use you should do this before trying to check for an anagram. It keeps the code modular and will make it easier to add features in the future.

If you are only looking for exact matches then first of all you don't need to test any dictionary word that doesn't have the same number of letters as your FinalArray (very badly named BTW, it should be called something like letters or anagram). For each word in your dictionary put the anagram letters in to a temporary List as Characters. Then iterate over the word you are checking and for each character try to remove a single instance of it from the letter list. At the end of the iteration if the list has no letters in it you have found a match otherwise you haven't.
 
Rob Rowan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you! This helps a lot! I'm new to java, learning it in university so the names of variables and arrays are just temp but I see why you mean about it having to be relevant. Ill try out what you said and get back to you. Big help thank you!
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!