I've been trying to tweak little bits of code here and there to make my output correct. I am trying to have my code be able to rearrange the letters in a word to make other words that exist in words.txt, from https://github.com/dwyl/english-words. Any help would be appreciated. Thanks.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;
public class WordsInWords {
public static void main(String[] args) throws IOException {
String file = "/Users/laptop/Desktop/Test.txt";
BufferedReader r = new BufferedReader(new FileReader(file));
StringBuilder b = new StringBuilder();
String c = r.readLine();
while (c != null) {
b.append(c);
b.append(" ");
c = r.readLine();
}
Scanner s = new Scanner(System.in);
String in = s.nextLine();
char[] input = new char[in.length()];
for (int i = 0; i < input.length; i++) {
input[i] = in.charAt(i);
}
char[] temp = null;
for (int i = 0; i < b.length(); i++) {
if (i < b.length() - 1 && b.charAt(i) == ' ' && b.charAt(i + 1) != ' ') {
boolean found = false;
int counter = 0;
while (!found) {
counter++;
if (b.charAt(i + counter) == ' ') {
found = true;
temp = new char[counter - 1];
for (int j = i + 1; j < i + counter; j++) {
temp[j] = b.charAt(j);
}
}
}
}
}
if (Arrays.asList(input).contains(temp)) {
System.out.println(temp);
}
}
}
Here is my tweaked code:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class WordsInWords {
public static void main(String[] args) throws IOException {
String file = "/Users/laptop/Desktop/words.txt";
BufferedReader r = new BufferedReader(new FileReader(file));
String[] words;
String c = r.readLine();
int a=0;
while (c != null) {
c = r.readLine();
a++;
}
words=new String[a];
a=0;
r = new BufferedReader(new FileReader(file));
String temp=r.readLine();
while (temp != null) {
words[a]=r.readLine();
temp=words[a];
a++;
}
for (int i = 0; i < words.length; i++) {
System.out.println(words[i]);
}
Scanner s = new Scanner(System.in);
String input = s.nextLine();
List<String> found = findRearranged(input, words);
System.out.println("For '" + input + "' found: " + Arrays.toString(found.toArray()));
}
public static List<String> findRearranged(String input, String[] words) {
List<String> found = new ArrayList<>();
for (String w : words) {
if (hasSameLetters(w, input)) {
found.add(w);
}
}
return found;
}
public static boolean hasSameLetters(String a, String b) {
if (a.length() != b.length()) {
return false;
}
while (a.length() > 0) {
for (char c : b.toCharArray()) {
int index = a.indexOf(c);
if (index >= 0) {
a = a.replace(String.valueOf(c), "");
} else {
return false;
}
}
}
return true;
}
}
Is rearranging the characters necessary? Rearranging the input and searching through dictionary to find equal word could take lot's of computing time, single few letter word can have many permutations.
For me, it looks like you want to find the words in dictionary for input word containing same letters (in other words, if the input word would be rearranged, it would give you the existing word in dictionary). Probably checking if both words are having exactly same letters, regardless their position in both strings should satisfy the requirement.
Here's the sample for that approach:
public class Sample {
public static void main(String[] args) {
//the words in dictionary
String[] words = {"words", "sword", "nord", "chord", "score", "cores", "mors", "xyz", "scores", "ordsw"};
String[] input = {"sword", "score", "tores", "nores"};
for (String i : input) {
List<String> found = findRearranged(i, words);
System.out.println("For '" + i + "' found: " + Arrays.toString(found.toArray()));
}
}
public static List<String> findRearranged(String input, String[] words) {
List<String> found = new ArrayList<>();
for (String w : words) {
if (hasSameLetters(w, input)) {
found.add(w);
}
}
return found;
}
public static boolean hasSameLetters(String a, String b) {
if (a.length() != b.length()) {
return false;
}
while (a.length() > 0) {
for (char c : b.toCharArray()) {
int index = a.indexOf(c);
if (index >= 0) {
a = a.replace(String.valueOf(c), "");
} else {
return false;
}
}
}
return true;
}
}
And this outputs in:
For 'sword' found: [words, sword, ordsw]
For 'score' found: [score, cores]
For 'tores' found: []
For 'nores' found: []
Edit: I see the assumption is that every word is in its own line. I saw that you already came up with counting the words in the file, but still in that case it's better to use Collections which are having dynamic size. Here's fixed sample:
public class WordsInWords {
public static void main(String[] args) throws IOException {
String file = "C:\\Users\\masta\\IdeaProjects\\podstawka-spring-java\\words.txt";
BufferedReader r = new BufferedReader(new FileReader(file));
List<String> words = new ArrayList<>();
String c = r.readLine();
while (c != null) {
words.add(c);
c = r.readLine();
}
for (int i = 0; i < words.size(); i++) {
System.out.println("Words: " + words.get(i));
}
Scanner s = new Scanner(System.in);
String input = s.nextLine();
List<String> found = findRearranged(input, words);
System.out.println("For '" + input + "' found: " + Arrays.toString(found.toArray()));
}
public static List<String> findRearranged(String input, List<String> words) {
List<String> found = new ArrayList<>();
for (String w : words) {
if (hasSameLetters(w, input)) {
found.add(w);
}
}
return found;
}
public static boolean hasSameLetters(String a, String b) {
if (a.length() != b.length()) {
return false;
}
while (a.length() > 0) {
for (char c : b.toCharArray()) {
int index = a.indexOf(c);
if (index >= 0) {
a = a.replace(String.valueOf(c), "");
} else {
return false;
}
}
}
return true;
}
}