Welcome to the Java Programming Forums


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.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Unscrambler, trying to get the last word of a dictionary...

  1. #1
    Member
    Join Date
    Oct 2011
    Posts
    35
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Unscrambler, trying to get the last word of a dictionary...

    Good evening all! I am having trouble with my following code. I am to enter a word, unscramble it and search a dictionary for the last word. i.e. I enter "tarp" and the dictionary has five words, "part", "prat", "rapt", "tarp", and "trap". I am only suppose to output the last word "trap" and having a problem at it. Can anyone help? You can also critique the code if you see a more efficient way of doing it. Please bear in mind, I realy do not want to have to rewrite the whole program so please make the critiques usefull in this sense.
    Here is the code:

    import java.io.*;
    import java.util.*;
     
    public class Unscrambler {
     
     
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		Scanner input = new Scanner(System.in);
    		System.out.print("Enter a word: ");
    		String str1 = input.nextLine();
    		char[] arrayOfChar1 = str1.toCharArray();
     
    		int i = str1.length();
     
    		String str2 = "/abcdefghijklmnopqrstuvwxyz";
    		char[] arrayOfChar2 = str2.toCharArray();
     
    		int[] arrayOfInt1 = new int[27];
    		int[] arrayOfInt2 = new int[27];
     
    		for (int j = 1; j < 27; j++) {
    			arrayOfInt1[j] = 0;
    		}
     
    		for (int j = 0; j < i; j++) {
    			for (int k = 1; k < 27; k++) {
    				if (arrayOfChar1[j] != arrayOfChar2[k])
    					continue;
    				arrayOfInt1[k] += 1;
    			}
    		}
     
    		try {
    			BufferedReader bf = new BufferedReader(new FileReader("c:\\dictionary.txt"));
    			String str3;
    			while ((str3 = bf.readLine()) != null) {
    				int m = str3.length();
    				char[] arrayOfChar3 = str3.toCharArray();
     
    				for (int n = 1; n < 27; n++) {
    					arrayOfInt2[n] = 0;
    				}
     
    				for (int n = 0; n < m; n++) {
    					for (int i1 = 1; i1 < 27; i1++) {
    						if (arrayOfChar3[n] != arrayOfChar2[i1])
    							continue;
    						arrayOfInt2[i1] += 1;
    					}
    				}
     
    				//int n = 0;
    				int i1 = 0;
     
    				for(int i2 = 1; i2 < 27; i2++) {
    					if (arrayOfInt2[i2] == arrayOfInt1[i2]) {
    						i1++;
    					}
    					if (arrayOfInt2[i2] > arrayOfInt1[i2])
    						continue;
    					//n++;
    				}
     
    				if (i1 == 26) {
     
    					System.out.print(" Maximum string found " + str3);
    				}
     
    				//if (n == 26) {
    					//System.out.print(" also " + str3);
    				//}
    			}
     
    			bf.close();
    		}
     
    		catch (IOException e) {
    			System.out.println("IO Error Occurred: " + e.toString());
     
    	}
     
    }
    }

    Oh! I have enclosed the dictionary.txt file in case anyone would actually like to run it. Thank You!
    Attached Files Attached Files


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,286
    My Mood
    Hungover
    Thanks
    142
    Thanked 620 Times in 531 Posts

    Default Re: Unscrambler, trying to get the last word of a dictionary...

    What does it return/print instead?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Oct 2011
    Posts
    35
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Unscrambler, trying to get the last word of a dictionary...

    Quote Originally Posted by KevinWorkman View Post
    What does it return/print instead?
    What I thought I said in the problem description, If I type in "tarp", it will actually print out, "part", "prat", "rapt", "tarp", and "trap". I need it to just print "trap." Sorry about that, I thought I made myself clear.

  4. #4
    Member
    Join Date
    Oct 2011
    Posts
    35
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Unscrambler, trying to get the last word of a dictionary...

    Ok I figured what my problem is. My while loop is printing what it matches, hence it prints the first value, loops then prints the next value until there are no more values. My question is how do I access "str3" outside of the while loop? Any help would be appreciated.

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,286
    My Mood
    Hungover
    Thanks
    142
    Thanked 620 Times in 531 Posts

    Default Re: Unscrambler, trying to get the last word of a dictionary...

    Quote Originally Posted by csharp100 View Post
    Ok I figured what my problem is. My while loop is printing what it matches, hence it prints the first value, loops then prints the next value until there are no more values. My question is how do I access "str3" outside of the while loop? Any help would be appreciated.
    You access it the same way you'd access any other variable, making sure that it's in scope. If you need it in scope outside the while loop, you have to declare it outside the while loop.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Unscrambler, trying to get the last word of a dictionary...

    Example 1:
    while(condition){
    int x=someIntegervalue;
    }
    PRINT x; // What should happen? Of course Error. Coz x is not outside while, in this case.
    Example 2:
    int x;
    while(condition){
    x=someIntegerValue;
    }
    PRINT x; // No error. Output will be the value in x

    Appended as the Kevin's answer.

Similar Threads

  1. Reading a text file word by word
    By dylanka in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: October 21st, 2011, 02:06 PM
  2. need details on Implementing Dictionary Filters for English words
    By madcrazyboys in forum Java Theory & Questions
    Replies: 2
    Last Post: May 6th, 2011, 12:53 PM
  3. [SOLVED] implementing a dictionary of words using an array of linked lists
    By McTown in forum Java Theory & Questions
    Replies: 3
    Last Post: April 27th, 2011, 02:19 PM
  4. Newbie: needs meaning of terms (dictionary)
    By boyscout in forum Member Introductions
    Replies: 1
    Last Post: March 29th, 2011, 05:34 AM
  5. Find the password with dictionary attack
    By mortis1572 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: January 19th, 2010, 09:07 PM