Dont be shy.
Thread: Help on my Java program |
In this program im trying to check to see if you can create a word with a given string.
for example
If the list was c,s,a,d,,f,g
Then a possible word i can make is sad.
The problem is have is if the list has mutliple letters example h,e,e,e,l,l,l,l,l,o
I want the program to output hello but my program outputs h e e l l l l o and i cant seem to make it so it only find hello
Heres the code :
***********Main class************Code:import java.util.ArrayList; import java.io.FileReader; import java.io.FileNotFoundException; import java.util.Scanner; import java.io.FileWriter; public class Words { Words(String x) { words = x; letters = new ArrayList<Character>(); } public void splitWord() { for(int x=0; x<words.length(); x++) { letters.add(words.charAt(x)); } } public void correctWords() { try { FileReader reader = new FileReader("Dictionary.txt"); Scanner in = new Scanner(reader); ArrayList<Character> empty = new ArrayList<Character>(); // while(in.hasNextLine()) //{ String line = in.nextLine(); for(int x=0; x<letters.size(); x++) { for(int y=0; y<line.length(); y++) { if(letters.get(x)==line.charAt(y)) { empty.add(letters.get(x)); letters.remove(x); } } } // } System.out.println(empty); } catch(FileNotFoundException e) { e.printStackTrace(); } } private String words; private ArrayList<Character> letters; }
/*****************DictionaryFile******…Code:public class WordTester { public static void main(String[] args) { Words w = new Words("heeelllllo"); w.splitWord(); w.correctWords(); } }
hello
aah
aahed
abases
abash
abashed
abashes
abashing
abasia
abasias
abasing
abatable
abate
abated
abater
Dont be shy.
So I put your program into a new project in Eclipse. I only changed a couple of things for readability, and had to find my own dictionary file. But I am wondering what output you get from your program on your side. I will attach my files. All I get for output is:
Looking at your code, it seems like you may be doing things in an order that you don't expect, which means you may want to re-think your algorithm a little. For example, for every word that you read from Dictionary.txt, you compare the letters of the given letter list to those of the next word. Rather than doing that here is what I propose:Code:Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 3, Size: 3 at java.util.ArrayList.RangeCheck(Unknown Source) at java.util.ArrayList.get(Unknown Source) at Words.correctWords(Words.java:44) at Main.main(Main.java:10)
- Set a lower limit for word size. For example set the limit to three, then you can easily ignore any words in the word list (Dictionary.txt) that are less than three characters long.
- Instead of looping through each letter of the given letter list (letters),
- Get a sub-list of all of the unique letters in the letter list (link: Fast way to get all unique value from a list in Java)
- Componentize the next word in the dictionary (for this I might change your splitWord method so that it takes an argument of type String and converts that String into ArrayList<Character>, and returns that.
Code:public ArrayList<Character> splitWord(String word) { ArrayList<Character> result = new ArrayList<Character>(); for(int charIndex = 0, max = word.length; charIndex < max; charIndex++) { result.add(word.charAt(charIndex); } return result; }- Then get only the unique values of the componentized word and loop through those to see whether they exist in the letter list by using ArrayList's contains(Object o) method (link: ArrayList::contains(Object o))
- Clearly then you need to ensure that any duplicates are taken care of. I'm certain you could think of a good way to handle this.
Overall, you are on the right track. Just think out what you want to do in order to find words that match the letters in the list. Think a little more about your algorithm. Think about how you would refine the list first, before you go and check each word if you were going to do it by hand. For example, I might take not of all of the unique letters in my letters list first, and then filter out any words that don't begin with those letters. You will have to keep the list in memory for this to be effective, or create a temporary file where you could store the new list before trying to determine complete matches. This is a swap-file-like pattern.
I hope that helps a little bit.
Edit: forgot the file initially - the Dictionary.txt file was too large and so is zip-archived. You'll need to unzip that if you want to use it, but I know you already have a dictionary file, so maybe you don't need it.
There are currently 1 users browsing this thread. (0 members and 1 guests)