Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This program takes a list of jumbled letters and makes them into the words using

ID: 3814369 • Letter: T

Question

This program takes a list of jumbled letters and makes them into the words using a jumbles.txt and a dictionary.txt(all the words in the dictionary). I can get it to do everything but for some reason the words are repeating themselves for some of the words and not for others?

****************************************************************************************************************************************

import java.io.*;
import java.util.*;

public class Project8 {
  
//List for Collecting permutations of word
static ArrayList<String> perms;
  
//This method calculates the permutations of the string using recursion and stores in the above list.
static void genPerms(char str[], int ind){
if(ind==str.length-1){
perms.add(String.valueOf(str));
return;
}
for(int i=ind;i<str.length;i++){
char ch = str[i];
str[i] = str[ind];
str[ind] = ch;
genPerms(str, ind+1);
ch = str[i];
str[i] = str[ind];
str[ind] = ch;
}
}
public static void main(String[] args) throws IOException {
//Reading the dictionary into a set
String dict = args[0];
BufferedReader f1 = new BufferedReader(new FileReader(new File(dict)));
String word;
HashSet<String> hs = new HashSet<String>();
while((word = f1.readLine())!=null){
hs.add(word);
}   
  
//Reading the words
String jumbles = args[1];
BufferedReader f2 = new BufferedReader(new FileReader(new File(jumbles)));
  
//List for storing final answer
ArrayList<String> fans = new ArrayList<>();
  
//Iterating through all the jumble words
while((word = f2.readLine())!=null){
//finding permutations
perms = new ArrayList<String>();
genPerms(word.toCharArray(),0);
String ans = word;
//Storing perms in a List
ArrayList<String> hor = new ArrayList<>();
for(String x : perms){
if(hs.contains(x)){
hor.add(x);
}
}
//Sorting the list
Collections.sort(hor);
//Calculating the answer
for(String x : hor){
ans = ans+" "+x;
}
fans.add(ans);
}
  
//Sorting the answer vertically
Collections.sort(fans);
for(String x : fans){
System.out.println(x);
}
}
}

*******************************************************************************************************************

My output:

addej jaded jaded
ahicryrhe hierarchy hierarchy hierarchy hierarchy
alvan naval naval
annaab banana banana banana banana banana banana banana banana banana banana banana banana
baltoc cobalt
braney nearby
celer creel creel
couph pouch
cukklen knuckle knuckle
dica acid cadi caid
dobeny beyond
dobol blood blood
dufil fluid
dupled puddle puddle
eslyep sleepy sleepy
ettniner renitent renitent renitent renitent renitent renitent renitent renitent
ettorp potter potter
genjal jangle
gluhc gulch
hartox thorax
hoybis boyish
hucnah haunch haunch
iddec diced diced
irrpo prior prior
kutbec bucket
lappor poplar poplar
lasia alias alias
laurib burial
lubly bully bully
meefal female female
milit limit limit
mopsie impose
mycall calmly calmly
nekel kneel kneel
nokte token
noper prone
nwae anew wane wean
nyegtr gentry
perrim primer primer
preko poker
pudmy dumpy
pypin nippy nippy
rebisc scribe
rodug gourd
rpeoims imposer promise semipro
shewo howes whose
wardty tawdry
warllc
yaldde deadly deadly

what its supposed to look like:

Explanation / Answer

/*In word banana there are several letters repeated. for Ex.n&a are
repeated several times.so permutations made same word with different combinations.
*/

import java.io.*;
import java.util.*;

public class Project8 {

//List for Collecting permutations of word
static ArrayList<String> perms;

//This method calculates the permutations of the string using recursion and stores in the above list.
static void genPerms(char str[], int ind){
if(ind==str.length-1){
perms.add(String.valueOf(str));
return;
}
for(int i=ind;i<str.length;i++){
char ch = str[i];
str[i] = str[ind];
str[ind] = ch;
genPerms(str, ind+1);
ch = str[i];
str[i] = str[ind];
str[ind] = ch;
}
}
public static void main(String[] args) throws IOException {
//Reading the dictionary into a set
String dict = args[0];
BufferedReader f1 = new BufferedReader(new FileReader(new File(dict)));
String word;
HashSet<String> hs = new HashSet<String>();
while((word = f1.readLine())!=null){
hs.add(word);
}

//Reading the words
String jumbles = args[1];
BufferedReader f2 = new BufferedReader(new FileReader(new File(jumbles)));

//List for storing final answer
ArrayList<String> fans = new ArrayList<>();

//Iterating through all the jumble words
while((word = f2.readLine())!=null){
//finding permutations
perms = new ArrayList<String>();
genPerms(word.toCharArray(),0);
String ans = word;
//Storing perms in a List
ArrayList<String> hor = new ArrayList<>();
for(String x : perms){
if(hs.contains(x)){
hor.add(x);
break;/*just break the loop after word matches with permutations to avoid multiple occurrences of word.*/
}
}
//Sorting the list
Collections.sort(hor);
//Calculating the answer
for(String x : hor){
ans = ans+" "+x;
}
fans.add(ans);
}

//Sorting the answer vertically
Collections.sort(fans);
for(String x : fans){
System.out.println(x);
f1.close();
f2.close();
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote