Ok so here is my code that I wrote, it basically lists the anagrams for a given input.
So how I did it was I read each line in my dictionary file, compared it to the anagram and 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 is some crits on my work and what I should/could have done differently. Obviously no one will check the whole section of code but it would be cool if people could just check sections of it.
This is only my second project so I want to learn 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