+ Reply to Thread
Results 1 to 3 of 3

Thread: Help on my Java program

  1. #1
    Newbie J_hollow is an unknown quantity at this point
    Join Date
    Apr 2010
    Posts
    6

    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 :
    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;
    }
    ***********Main class************

    Code:
    public class WordTester
    {
    	public static void main(String[] args)
    	{
    		
    	               Words w = new Words("heeelllllo");
    		w.splitWord();
    		w.correctWords();
    	}
    
    }
    /*****************DictionaryFile******…
    hello
    aah
    aahed
    abases
    abash
    abashed
    abashes
    abashing
    abasia
    abasias
    abasing
    abatable
    abate
    abated
    abater

  2. #2
    Newbie J_hollow is an unknown quantity at this point
    Join Date
    Apr 2010
    Posts
    6

    Re: Help on my Java program

    Dont be shy.

  3. #3
    Newbie Nathandelane is an unknown quantity at this point Nathandelane's Avatar
    Join Date
    Sep 2007
    Location
    Utah, USA
    Posts
    22
    Blog Entries
    1

    Smile Re: Help on my Java program

    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:

    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)
    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:
    1. 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.
    2. Instead of looping through each letter of the given letter list (letters),
      1. 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)
      2. 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;
        }
      3. 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))
      4. 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.
    Bijgevoegde bestanden

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. Java chat program
    By lintwurm in forum Java Help
    Replies: 2
    Last Post: 03-27-2010, 04:02 PM
  2. First and Hardest Java Program
    By Dyroxide in forum Java Help
    Replies: 4
    Last Post: 02-23-2009, 07:51 PM
  3. Need Help With Java Program
    By coala2000 in forum Java Help
    Replies: 3
    Last Post: 12-28-2008, 05:42 AM
  4. Help anyone pls!! Java program
    By DexterHol in forum Java Help
    Replies: 1
    Last Post: 11-20-2007, 11:51 AM
  5. First Java Program and .....
    By Void in forum Java Help
    Replies: 1
    Last Post: 10-03-2006, 04:27 PM