File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes ignoring space in an anagram solver Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "ignoring space in an anagram solver" Watch "ignoring space in an anagram solver" New topic
Author

ignoring space in an anagram solver

Joanna Spence
Greenhorn

Joined: May 31, 2010
Posts: 23
I am building an anagram solver and some of the test files contain phrases and I need to know how to 'ignore" the spaces?
I tried the .trim() function but i'm still getting the same errors...

any help would be appreciated.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29261
    
140

Elmira,
You would either have to loop through the string to remove blanks or use a regular expression to remove any whitespace.

Trim only handles whitespace at the beginning or end of a string. The two above methods handle it anywhere.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Joanna Spence
Greenhorn

Joined: May 31, 2010
Posts: 23
i figured out if i use the .replace() function with string.replace(" ","") it removes the spaces from in between the words, but now my anagram solver is having trouble finding matches because the string is all the words bunched together.

How would i ignore the space but still be able to successfully search for anagrams?
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 743
Hi,

Can you give a concrete example of what you want to do.
Its not very clear for me, but I reckon I can help you.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36520
    
  16
Welcome to the Ranch

As you have been told on your other thread, you need to work out what algorithm you are using to see whether words are anagrams of each other. Write down carefully what you are doing, until you get it all in words of one syllable. You need a pencil, paper and eraser

The eraser is the most important piece of hardware
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10918
    
  12

The problem is not well defined - at least to us. It's hard (impossible/a waste of time) to offer suggestions without knowing what you're really going for. Please define the problem more clearly, and we'll be happy to help.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Joanna Spence
Greenhorn

Joined: May 31, 2010
Posts: 23
I am building an anagram solver which takes an input word, and searches the dictionary based on the prefixes of the word..

It has to use recursion and backtracking and ignore spaces in between words.

I came up with a method for this, but not sure if it will work out correctly.

In the beginning of the program, i read in a dictionary list of words into dictionaryList, and the test file from the command line into testList

Then I used the stringtokenizer to create an ArrayList of all the letters contained in each word called letterList.

The letterList would be used to iterate through and create prefixes to search the dictionary on...

I would use the method I outlined below to search for anagrams..





Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18123
    
  39

Elmira Love wrote:i figured out if i use the .replace() function with string.replace(" ","") it removes the spaces from in between the words, but now my anagram solver is having trouble finding matches because the string is all the words bunched together.

How would i ignore the space but still be able to successfully search for anagrams?


IMHO, the easiest solution is not to permanently remove the spaces. Keep working with the string that has the spaces, and hence, has the words. Only when you need to confirm whether it is an anagram to do make a new (and temporary) string, with the spaces removed, that you can check for anagrams. When you are done, throw away the string without the spaces -- and go back to the original string.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ignoring space in an anagram solver
 
Similar Threads
Recursive anagram problem
Anagram contest
Play Anagram