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 4 of 4

Thread: Java word unscrambler

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java word unscrambler

    I just can't figure out whats wrong with this code. It compiles but just outputs no answer! The desired function is for a user to enter nicegroan for example, and for the java program to run through dictionary.txt and find the longest word it can make from the user input which in this case would be ignorance. Any help would be appriciated.

     
         import java.util.Scanner;
     
    public class countdowntwo
    {
     
    public static void main(String[] args){
     
            //instantiate an instance of FileIO    
            FileIO reader = new FileIO();
     
            //the code below shows how to load a file
            String[] contents = reader.load("C:\\dictionary.txt");
     
            //now you can process the contents of the file as a String array
     
            Scanner in = new Scanner(System.in);
     
            System.out.println("Insert your letters");
            String input = in.nextLine();
            char[] inputArray = input.toCharArray();
     
            for(int i = 0; i < contents.length; i++)//loop through dictionary array
            {
            	for(int j = 0; j < contents[i].length()-1; j++)//loop through contents of that array
    			{
            		char[] word = contents[i].toCharArray();//create a character array for each word in file
     
            		for(int k = 0; k < word.length-1; k++)//loop through character array
    				{
            			while(k<inputArray.length)//while its less then user input
    					{
            			   if(word[k]==inputArray[k])//if letters in word is equal to user input
    					    {
            				  word[j]=' ';
            				    for(int l = 0; l < word.length-1; l++)
    						   {
            					if(word[l]!= ' ')
    							{
            						break;
            					}
            					else
    							{
            						System.out.println("The word is "+contents[i]);
            						break;
            					}
     
     
            				}
            			}
            			}
     
            		}
            	}
     
     
            }
     
     
     
        }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java word unscrambler

    outputs no answer!
    Try debugging the code by adding some println methods that print out the values of the variables used to control the logic and execution flow so you can see what the computer sees when it executes the code. The printout should show you where the logic is going wrong.


    The posted code does not compile because of missing class definitions.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java word unscrambler

    The posted code compiles for me fine in eclipse!

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java word unscrambler

    Then you will be able to:
    Try debugging the code by adding some println methods that print out the values of the variables used to control the logic and execution flow so you can see what the computer sees when it executes the code. The printout should show you where the logic is going wrong.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 5
    Last Post: August 20th, 2012, 01:01 AM
  2. read a file word by word
    By poornima2806 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: February 23rd, 2012, 03:14 PM
  3. Unscrambler, trying to get the last word of a dictionary...
    By csharp100 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 1st, 2011, 03:44 AM
  4. 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
  5. Java classes and the 'new' word
    By Scotty in forum Java Theory & Questions
    Replies: 1
    Last Post: March 27th, 2011, 11:05 AM

Tags for this Thread