Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Given two files

random_letters.txt

AABBBBB
FLOWERS
BACKGFD
TOBEACH

dictionary.txt

flowers
to
beach
back

I need to check each combination of the random_letters with dictionary to see if there is anything common. it can be a word with at least 6 characters or two words that equal at least 6 characters. Which would make it FLOWERS or TOBEACH.

I am having a tough time figuring out what I need to do. I can get it to work for words with 7 characters because I used strings. I understand I need to use char in order for it to work.

what I have so far:

public static void compare() {

private static String stringToWrite2 = "";
private static String[] allWords = new String[2187];
private static String[] diction = new String[79765];
private static char[][] test = new char[2187][7];
private static char[][] test2 = new char[79765][7];
  public static void main(String args[])
    try {
        Scanner file1 = new Scanner(new File("random_letters.txt"));
        Scanner file2 = new Scanner(new File("dictionary.txt"));

        for(int i = 0; i < 2187; i++) {
            allWords[i] = file1.next();
            test[i] = allWords[i].toCharArray();
        }


        for(int i = 0; i < 79765; i++) {
            diction[i] = file2.next();
            diction[i] = diction[i].toUpperCase();
            test2[i] = diction[i].toCharArray();
        }

        for(int i = 0; i < 2187; i++) {
            for (int j = 0; j < 79765; j++) {
                if(allWords[i].equals(diction[j])) {
                    stringToWrite2 += diction[j];
                }
            }
        }
    } catch (IOException e) {
        System.out.println("could not find file");
    } 
    System.out.println("-------------------");
    System.out.println(stringToWrite2);

    for(int i = 0; i < 6; i++) {
        for (int j = 0; j < 7; j++)
            System.out.println(test2[i][j]);
    }

}}
share|improve this question

put on hold as off-topic by chrylis, Bohemian 15 hours ago

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Bohemian
If this question can be reworded to fit the rules in the help center, please edit the question.

    
What have you tried so far? Please read How do I ask a good question?. Post your code, see How to create a Minimal, Complete, and Verifiable example. –  DavidPostill 17 hours ago
    
With the code it's more more understandable, thanks ! –  Jérôme Teisseire 16 hours ago
    
Hard coding those for-loop iterations is a really bad idea... if a line get deleted off one of the files an error's probably going to be thrown. Try using a while loop and reading the next line so long as its there instead. –  Mshnik 16 hours ago
    
By "each combination of the random_letters", do you mean that the strings in random_letters can be re-ordered to match a word in dict? If so you're looking at a lot of work, depending on the length of strings in random_letters. –  Mshnik 16 hours ago
    
@Mshnik no they have to stay in the same order –  UnhinderedLimpidity 16 hours ago

1 Answer 1

You have two somewhat distinct tasks here: determining if there are any words in dictionary that are also in random_letters (of length >= 6), and determining if there are any sets of two words in dictionary such that their union is a word in random_letters.

Instead of using an array, let's use HashSets for storage, because the single most used operation here will probably be .contains(...). It also gives us access to .retainAll(...), which is very useful for finding intersections.

For the second half of the task, my initial thought was to create a data structure with all of the pairwise permutations of words in diction, and intersect that with allWords. I quickly realized how big that would (likely) become. Instead I used an uglier but more space efficient solution.

private static HashSet<String> allWords = new HashSet<String>();
private static HashSet<String> diction = new HashSet<String>();

public static void compare() {
  try {
    Scanner file1 = new Scanner(new File("random_letters.txt"));
    Scanner file2 = new Scanner(new File("dictionary.txt"));

    for(int i = 0; i < 2187; i++) {
      allWords.add(file1.next());
    }

    for(int i = 0; i < 79765; i++) {
      diction.add(file2.next().toUpperCase());
    }

    //Compile set of words that are in both
    HashSet<String> intersect = new HashSet<String>();
    intersect.addAll(allWords);
    intersect.retainAll(diction);

    for (String s : intersect){
        System.out.println(s);
    }

    //For every word in random_letters, see if there is a word in diction that is the start of it
    HashSet<String> couplesIntersect = new HashSet<String>();
    for(String s : allWords){
        for(String d : diction){
            if(s.startsWith(d)){
                //If so, check every word in diction again to see if there is a word that is the remainder
                String remainder = s.subString(d.length());
                for(String d2 : diction){
                    if(d2.equals(remainder))
                        //If so, store this word
                        couplesIntersect.add(s);
                }
            }
        }
     }

     //Print those results
     for (String s : couplesIntersect){
         System.out.println(s);
     }

  } catch (IOException e) {
    System.out.println("could not find file");
  } 
 }
}
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.