Need a little help with printing last string...
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Need a little help with printing last string...

  1. #1
    Join Date
    Oct 2011
    Posts
    5

    Need a little help with printing last string...

    Hello everyone, I have a problem with my code. I am trying to print the last alphabetical word from an anagram unscrambler. What happens is I enter the characters "umsl." The words are unscrambled and compared to a dictionary and prints out "lums" and then prints "slum" All I need is to print "slum." I have enclosed my code.

    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;
    			}
    		}
    			String str3 = null;
    		try {
    			BufferedReader bf = new BufferedReader(new FileReader("c:\\dictionary.txt"));
    			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 i1 = 0;
    
    				for(int i2 = 1; i2 < 27; i2++) {
    					if (arrayOfInt2[i2] == arrayOfInt1[i2]) {
    						i1++;
    					}
    				}
    
    				if (i1 == 26) {
    
    					System.out.print(" Maximum string found " + str3 );
    
    				}
    			}
    
    			bf.close();
    		}
    
    		catch (IOException e) {
    			System.out.println("IO Error Occurred: " + e.toString());
    		}
    
    }
    }
    Anyone please help.

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,474

    Re: Need a little help with printing last string...

    Which line in the posted code prints out the words?
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Aug 2011
    Posts
    10

    Re: Need a little help with printing last string...

    Your code is remarkably close to being a correct solution.

    This code prints all the matching words:

    Code:
    				if (i1 == 26) {
    					System.out.print(" Maximum string found " + str3 );
    				}
    And it is in a loop.

    So now you have to figure out how, in a loop, to find and keep track of the largest item you've found. You might start with a String value that is smaller than all known words. The empty string ("") would work for this. Then you need to compare two string values to see which one is "larger." Consider the 'compareTo' method on the String class:

    http://download.oracle.com/javase/6/...a.lang.String)


    (Eventually, you'll want to get rid of the print statement shown above, because it's misleading. You'll have a print statement after the end of the loop.)

  4. #4
    Join Date
    Oct 2011
    Posts
    5

    Re: Need a little help with printing last string...

    Thanks JeffGrigg! I did figure that part out. I did not read my paperwork very well so now I have a couple of problems, first is if I put the following statement:
    Code:
    if (str1 == "END"){
         System.out.println("Program will exit");
         System.exit(0);
    }
    after the string is entered into the array, it does absolutely nothing. My second thing is I need o be able to enter another "word" after the program does not find a word. For instance, if I enter the word "sailplane", it or any permutation of the word is not found so I need to be able to have the program reenter another word. Here is what I have so far and any kinda code start will help. Thanks to all.

    Code:
    import java.io.*;
    import java.util.*;
    
    public class Pe2 {
    
    	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;
    			}
    		}
    			String str3 = null;
    			String str4 = null;
    		try {
    			BufferedReader bf = new BufferedReader(new FileReader("c:\\dictionary.txt"));
    			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 i1 = 0;
    
    				for(int i2 = 1; i2 < 27; i2++) {
    					if (arrayOfInt2[i2] == arrayOfInt1[i2]) {
    						i1++;
    					}
    				}
    
    				if (i1 == 26) {
    					str4 = str3;
    
    				}
    				
    				
    			}
    
    			bf.close();
    		}
    		
    		catch (IOException e) {
    			System.out.println("IO Error Occurred: " + e.toString());
    		}
    		
    		//System.out.println(str4);
    		if (str4 == null){
    			System.out.println("String not found");
    			
    		}
    		else {
    			System.out.println(str4);
    		}
    
    }
    }

  5. #5
    Join Date
    Aug 2011
    Location
    West Yorkshire, U.K.
    Posts
    54

    Re: Need a little help with printing last string...

    if (str1 == "END"){
    is not the correct way to perform string equality tests. Use:
    Code:
    if (str1.equals("END"))  {

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center