0

My bad for the title, I am usually not good at making those.

I have a programme that will generate all permutations of an inputted word and that is supposed to check to see if those are words (checks dictionary), and output the ones that are. Really I just need the last the part and I can not figure out how to parse through a file.

I took out what was there (now displaying the "String words =") because it really made thing worse (was an if statement). Right now, all it will do is output all permutations.

Edit: I should add that the try/catch was added in when I tried turning the file in a list (as opposed to the string format which it is currently in). So right now it does nothing. One more thing: is it possible (well how, really) to get the permutations to display permutations with lesser characters than entered ? Sorry for the bad wording, like if I enter five characters, show all five character permutations, and four, and three, and two, and one.

import java.util.List;
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import static java.lang.System.out;

public class Permutations
{
  public static void main(String[] args) throws Exception
  {
    out.println("Enter anything to get permutations: ");
    Scanner scan = new Scanner(System.in);
    String io = scan.nextLine();
    String str = io;
    StringBuffer strBuf = new StringBuffer(str);
    mutate(strBuf,str.length());
  }

  private static void mutate(StringBuffer str, int index)
  {
    try
    {
      String words = FileUtils.readFileToString(new File("wordsEn.txt"));

      if(index <= 0)
      {
        out.println(str);
      }

      else
      {
        mutate(str, index - 1);
        int currLoc = str.length()-index;
        for (int i = currLoc + 1; i < str.length(); i++)
        {
          change(str, currLoc, i);
          mutate(str, index - 1);
          change(str, i, currLoc);
        }
      }
    }
    catch(IOException e)
    {
      out.println("Your search found no results");
    }
  }
  private  static void change(StringBuffer str, int loc1, int loc2)
  {
    char t1 = str.charAt(loc1);
    str.setCharAt(loc1, str.charAt(loc2));
    str.setCharAt(loc2, t1);
  }
} 

1 Answers1

1

If each word in your file is actually on a different line, maybe you can try this:

BufferedReader br = new BufferedReader(new FileReader(file));  
String line = null;  
while ((line = br.readLine()) != null)  
{  
    ... // check and print here
} 

Or if you want to try something else, the Apache Commons IO library has something called LineIterator.

An Iterator over the lines in a Reader. LineIterator holds a reference to an open Reader. When you have finished with the iterator you should close the reader to free internal resources. This can be done by closing the reader directly, or by calling the close() or closeQuietly(LineIterator) method on the iterator.

The recommended usage pattern is:

LineIterator it = FileUtils.lineIterator(file, "UTF-8");
    try {
        while (it.hasNext()) {
            String line = it.nextLine();
            // do something with line
        }
    } finally {
        it.close();
}
JNevens
  • 11,202
  • 9
  • 46
  • 72
  • RE: your edit about speed, this question may be of interest: http://stackoverflow.com/q/22955178/1267663 – Whymarrh Apr 16 '14 at 18:06
  • @JN11 Ah, sorry. I had to go for a bit. It _should_ work, but everything I have tried so far with getting it to check and print does not work. – user3542198 Apr 16 '14 at 20:55