What I would like to do is take an input txt file of any length which looks like this
bob
joe
obb
oej
and produce an output txt file which sorts groups of re-arranged words on a single line and alphabetically in an output txt file.
bob obb
joe oej
Here is what I have attempted so far where args[0]
is a file called input.txt passed in the command line.
public static void main(String[] args) {
File file = new File(args[0]):
Scanner scan = new Scanner(file);
List<char[]> anagrams = new ArrayList();
while (scan.hasNextLine()) {
Scanner scan2 = new Scanner(file);
String line = scan.nextLine();
char[] arr = line.toCharArray();
if (containsAnagram(anagrams, line))
continue;
else anagrams.add(line);
while (scan2.hasNextLine()) {
String line2 = scan2.nextLine();
if (isAnagram(arr, line2))
fileContent2+=” ”+line2;
}
fileContent+=fileContent2+”\n”;
}
}
private static boolean isAnagram(char[] arr, String line) {
for (int i=0; i<arr.length; i++) {
if (!Arrays.asList(line).contains(arr(i))
break;
if (i=arr.length-1)
return true;
}