In: Computer Science
Part 3: Anagram Arranger (20 pts)
Overview
Specifications
Welcome to the Anagram Arranger!
Please enter the name of your input file: spooky.txt
Word #1 is zombie
1: z
2: o
3: m
4: b
5: i
6: e
Enter the position numbers of the two letters you wish to swap: 1
2
z o m b i e
^ ^
Are these the letters you wish to swap? (y/n): y
The new word is: o z m b i e
Want to keep rearranging? (y/n): y
1: o
2: z
3: m
4: b
5: i
6: e
Enter the position numbers of the two letters you wish to swap: 3
5
o z m b i e
^
^
Are these the letters you wish to swap? (y/n): y
The new word is: o z i b m e
Want to keep rearranging? (y/n): n
Word #2 is mummy
1: m
2: u
3: m
4: m
5: y
Enter the position numbers of the two letters you wish to
swap: etc...
A complete Example:
This example assumes an input file named words.txt with the following content - however, the user should be able to enter any file name and the file can contain any number of words:
Spring
Summer
Fall
Winter
Output of running Anagram.java
Welcome to the Anagram Arranger!
Please enter the name of your input file: abc.txt
Sorry. I cannot find a file by that name!
Please enter the name of a valid input file: 123.txt
Sorry. I cannot find a file by that name!
Please enter the name of a valid input file: words.txt
Word #1 is Spring
1: S
2: p
3: r
4: i
5: n
6: g
Enter the position numbers of the two letters you wish to swap: 3
5
S p r i n g
^
^
Are these the letters you wish to swap? (y/n): y
The new word is: S p n i r g
Want to keep rearranging? (y/n): yes
1: S
2: p
3: n
4: i
5: r
6: g
Enter the position numbers of the two letters you wish to swap: 1
6
S p n i r g
^
^
Are these the letters you wish to swap? (y/n): yes
The new word is: g p n i r S
Want to keep rearranging? (y/n): n
Word #2 is Summer
1: S
2: u
3: m
4: m
5: e
6: r
Enter the position numbers of the two letters you wish to swap: 2
3
S u m m e r
^ ^
Are these the letters you wish to swap? (y/n): Yes
The new word is: S m u m e r
Want to keep rearranging? (y/n): Yes
1: S
2: m
3: u
4: m
5: e
6: r
Enter the position numbers of the two letters you wish to swap: 4
5
S m u m e r
^
^
Are these the letters you wish to swap? (y/n): y
The new word is: S m u e m r
Want to keep rearranging? (y/n): n
Word #3 is Fall
1: F
2: a
3: l
4: l
Enter the position numbers of the two letters you wish to swap: 1
2
F a l l
^ ^
Are these the letters you wish to swap? (y/n): y
The new word is: a F l l
Want to keep rearranging? (y/n): n
Word #4 is Winter
1: W
2: i
3: n
4: t
5: e
6: r
Enter the position numbers of the two letters you wish to swap: 1
3
W i n t e r
^ ^
Are these the letters you wish to swap? (y/n): n
1: W
2: i
3: n
4: t
5: e
6: r
Enter the position numbers of the two letters you wish to swap: 1
4
W i n t e r
^ ^
Are these the letters you wish to swap? (y/n): y
The new word is: t i n W e r
Want to keep rearranging? (y/n): y
1: t
2: i
3: n
4: W
5: e
6: r
Enter the position numbers of the two letters you wish to swap: 2
5
t i n W e r
^
^
Are these the letters you wish to swap? (y/n): y
The new word is: t e n W i r
Want to keep rearranging? (y/n): y
1: t
2: e
3: n
4: W
5: i
6: r
Enter the position numbers of the two letters you wish to swap: 3
4
t e n W i r
^ ^
Are these the letters you wish to swap? (y/n): y
The new word is: t e W n i r
Want to keep rearranging? (y/n): n
Bye!
Corresponding output.txt for above
example:
g p n i r S
S m u e m r
a F l l
t e W n i r
Required Error Checking
Sorry. I cannot find a file by that name!
Please enter the name of a valid input file:
Error Checking Example:
Welcome to the Anagram Arranger!
Please enter the name of your input file: ddd.txt
Sorry. I cannot find a file by that name!
Please enter the name of a valid input file: nnn.txt
Sorry. I cannot find a file by that name!
Please enter the name of a valid input file: splooky.txt
Sorry. I cannot find a file by that name!
Please enter the name of a valid input file: spooky.txt
Word #1 is zombie
1: z
2: o
3: m
4: b
5: i
6: e
Enter the position numbers of the two letters you wish to swap: 4
2
Invalid entry!
1: z
2: o
3: m
4: b
5: i
6: e
Enter the position numbers of the two letters you wish to swap: 1
10
Invalid entry!
1: z
2: o
3: m
4: b
5: i
6: e
Enter the position numbers of the two letters you wish to swap: 2
2
Invalid entry!
1: z
2: o
3: m
4: b
5: i
6: e
Enter the position numbers of the two letters you wish to swap: 2
3
z o m b i e
^ ^
Are these the letters you wish to swap? (y/n): yes
The new word is: z m o b i e
Want to keep rearranging? (y/n): etc.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class Anagram {
private static final String FILENAME = "anagram_words.txt";
public static void main(String[] args) {
ArrayList<String> words = readWordsFromFile(FILENAME);
for(int i = 0; i < words.size(); i++)
{
String word = words.get(i);
System.out.println("Word #" + (i + 1) + " is " + word +
"\n");
performAnagram(word);
System.out.println();
}
System.out.println("Bye!");
System.exit(0);
}
private static ArrayList<String> readWordsFromFile(String
filename)
{
ArrayList<String> words = new ArrayList<>();
Scanner fileReader;
try
{
fileReader = new Scanner(new File(filename));
while(fileReader.hasNextLine())
{
words.add(fileReader.nextLine().trim());
}
fileReader.close();
}catch(FileNotFoundException fnfe){
System.out.println(filename + " could not be found!
Exiting..");
System.exit(0);
}
return words;
}
private static void performAnagram(String s)
{
char continueChoice = 0;
do
{
Scanner sc = new Scanner(System.in);
char chars[] = s.toCharArray();
for(int i = 0; i < chars.length; i++)
System.out.println((i + 1) + ": " + chars[i]);
System.out.print("Enter the position numbers of the two letters
you wish to swap: ");
String str = sc.nextLine().trim();
String[] posData = str.split(" ");
int pos1 = Integer.parseInt(posData[0]) - 1;
int pos2 = Integer.parseInt(posData[1]) - 1;
if(pos1 < 0)
pos1 = 0;
if(pos2 < 0)
pos2 = 0;
if(!(pos1 >= 0 && pos1 <= str.length() - 1)
&& !(pos2 >= 0 || pos2 <= str.length() - 1))
System.out.println("Invalid entry!\n");
else
{
String modStr = s + "\n" + getIndicator(s.length(), pos1,
pos2);
System.out.println(modStr);
System.out.print("Are these the letters you wish to swap? (y/n):
");
char swapChoice = sc.nextLine().trim().charAt(0);
if(swapChoice == 'N' || swapChoice == 'n')
continue;
// if yes
String swappedStr = swapStr(s, pos1, pos2);
System.out.println("The new word is: " + swappedStr);
s = swappedStr;
System.out.print("\nWant to keep rearranging? (y/n): ");
continueChoice = sc.nextLine().trim().charAt(0);
if(continueChoice == 'N' || continueChoice == 'n')
break;
System.out.println();
}
}while(continueChoice != 'N' || continueChoice != 'n');
}
private static String swapStr(String s, int pos1, int pos2)
{
String newStr = "";
char chars[] = s.toCharArray();
char temp = chars[pos1];
chars[pos1] = chars[pos2];
chars[pos2] = temp;
for(int i = 0; i < chars.length; i++)
newStr += chars[i];
return newStr;
}
private static String getIndicator(int len, int pos1, int
pos2)
{
String newStr = "";
for(int i = 0; i < len; i++)
{
if(i == pos1 || i == pos2)
newStr += "^";
else
newStr += " ";
}
return newStr;
}
}
********************************************************* SCREENSHOT *******************************************************
INPUT FILE (angram_words.txt) - This file needs to be created before running the code and this code should be created within the same project directory where the above Anagram.java file will be residing.
CONSOLE OUTPUT :