1 Attachment(s)
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:
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!
Re: Unscrambler, trying to get the last word of a dictionary...
What does it return/print instead?
Re: Unscrambler, trying to get the last word of a dictionary...
Quote:
Originally Posted by
KevinWorkman
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.
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.
Re: Unscrambler, trying to get the last word of a dictionary...
Quote:
Originally Posted by
csharp100
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.
Re: Unscrambler, trying to get the last word of a dictionary...
Example 1:
Code :
while(condition){
int x=someIntegervalue;
}
PRINT x; // What should happen? Of course Error. Coz x is not outside while, in this case.
Example 2:
Code :
int x;
while(condition){
x=someIntegerValue;
}
PRINT x; // No error. Output will be the value in x
Appended as the Kevin's answer.