|
-
October 29th, 2011, 06:44 PM
#1
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|