Skip to main content
5 of 5
replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/

Anagrams for a given input

This question is the first draft, my new revised code is here: Anagrams for a given input 2.0

Here is my code that I wrote, which basically lists the anagrams for a given input.

I did this by reading each line in my dictionary file and comparing it to the anagram. Only if it matched did I add it to the list, instead of adding all the words to the list and then sorting through them.

I compared the words by first checking the length of both words, and then if they matched I put put each word into a list and then sorted the list, then I compared the two lists using the equals() function. And iterated my counter.

The function of my getOutPut() was just to neaten up the printout.

What I would like are some critiques on my work and what I should/could have done differently, especially pertaining to semantic efficiency, code correctness, common practices that I may be unaware of, and code efficiency.

This is only my second project, so I want to learn from it to become a better programmer. That's why I'm asking.

The methods are in the order they are called in:

(Note that I split the code up to make it easier to see, all the methods are inside the Anagramatic Class)

package anagramatic;

/**
 * IDE : NETBEANS
 * Additional Libraries : Guava
 * @author KyleMHB
 */
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
import javax.swing.JOptionPane; 

public class Anagramatic {
static int k;
public static void main(String[] args) throws FileNotFoundException{
    String anagram;
    anagram=input();
    ArrayList<String> words=readFile(anagram);
    String output= getOutPut(words);
    JOptionPane.showMessageDialog(null,
            "The anagram "+anagram+" has "+k+" matches, they are:\n\n"+output);
}//PVSM
private static String input() {
    String input;
    input = JOptionPane.showInputDialog(null,
     "Enter the Word or words you would like to be processed");
    return input;
}//input
private static ArrayList readFile(String anag) throws FileNotFoundException{
    k=0;
    ArrayList<String> list;
    ArrayList<Character> a;
    ArrayList<Character> b;
    try (Scanner s = new Scanner(new File("words.txt"))){
        list = new ArrayList<>();
        while (s.hasNext()){
            String word= s.next();
            if (word.length()==anag.length()){  
                a = new ArrayList<>();
                b = new ArrayList<>();
                for(int i=0;i!=word.length();i++){
                    a.add(anag.charAt(i));
                    b.add(word.charAt(i));
                }//forloop to make two lists of the words
                Collections.sort(a);
                Collections.sort(b);
                if(a.equals(b)){
                    list.add(word);
                    k++;
                }//comparing the two lists
             }//if length
          }//while
       }//try
    return list;
}//readfile
private static String getOutPut(ArrayList<String> words) {
    String wordz="[";
    int x=0;
    int y=0;
    for(int i=0; i!=words.size()-1;i++){
        if(x!=7){
            wordz+=words.get(i)+", ";
            x++;
        }else{wordz+="\n";x=0;}
    }//for
    wordz+=words.get(words.size()-1)+"]";
return wordz;
}//getOutPut
}//Anagramatic
Dinocv
  • 381
  • 1
  • 2
  • 14