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!