In: Computer Science
Problem: Implement a class named StringParser, along with specified methods, that can read words from a text file, parse those words (such as finding palindromes), and write those words to a new file More specifically: 1. Write the StringParser class so that it has the following methods: a) public static void main(String[] args) -- The main driver of the program. Prompts the user for an input file, an output file, and a choice for what kinds of words to output. b) public static void rawOutput(File in, File out)-- Parses an input file, writing each word from the input on a separate line in the output file. c) public static void palindromeOutput(File in,File out) -- Parses an input file, writing only the palindromes to the output file, in alphabetical order, one line at a time, without repetition. Palindromes are words that read the same forward and backward, ignoring digits and punctuation. d) public static void hundredDollarWordOutput(File in, File out) -- Parses an input file, writing only the $100 words to the output file, in alphabetical order, one line at a time, in uppercase, without repetition. $100 words are found by assigning $1 to A, $2 to B, and so on. For example ELEPHANTS is a $100 word because: E + L + E + P + H + A + N + T + S is 5 + 12 + 5 + 16 + 8 + 1 + 14 + 20 + 19 = 100 e) public static boolean isPalindrome(String word) -- Determines if a word is a palindrome (reads the same forward and backward). The method is case-sensitive, so it will say that dad and DAD and d-a-d are palindromes, but Dad and d-ad are not palindromes. f) public static String cleanup(String word)-- Takes a string and removes all non-letters, returning an all uppercase version. For example, this input: "A man, a plan, a canal. Panama." will produce this output: "AMANAPLANACANALPANAMA" g) public static int wordValue(String word)-- Returns the monetary value of a word, found by assigning the value $1 to A, $2 B, and so on, up to $26 for Z. The method will ignore differences in case, so both A and a are each worth $1. It will also ignore any non-letters in the input. 2. Create your own input test file, and use it with your code to generate three output files. 3. Upload all five files (Java source code, input file, three output files).
Complete Program:
File: StringParser.java
Input file: indata.txt
Sample Run 1:
Sample Run 2:
Output file: rawWords.txt
Sample Run 3:
Output file: palindromeWords.txt
Sample Run 4:
Output file: hundredDollarWords.txt
CODE TO COPY:
File: StringParser.java
// StringParser class implementation
import java.io.*;
import java.util.*;
public class StringParser
{
// start main method
public static void main(String args[])
{
Scanner keyboard = new
Scanner(System.in);
String inputFileName;
String outputFileName;
File in = null;
File out = null;
int choice;
System.out.print("Enter input
filename: ");
inputFileName =
keyboard.next();
in = new File(inputFileName);
if (in.exists())
{
System.out.println("Found. What do you want to output?");
System.out.println("1. Raw word list");
System.out.println("2. Palindromes");
System.out.println("3. $100 words");
System.out.print("Choose: ");
choice =
keyboard.nextInt();
if(choice >=
1 && choice <= 3)
{
System.out.print("Enter output filename:
");
outputFileName = keyboard.next();
out = new File(outputFileName);
}
switch
(choice)
{
case 1:
rawOutput(in, out);
break;
case 2:
palindromeOutput(in, out);
break;
case 3:
hundredDollarWordOutput(in, out);
break;
default:
System.out.println("Invalid choice!");
}
}
else
{
System.out.println("File does not exist. Goodbye.");
}
keyboard.close();
} // end of main method
// rawOutput method implementation
public static void rawOutput(File in, File out)
{
try
{
Scanner infile =
new Scanner(in);
PrintWriter
outfile = new PrintWriter(out);
while
(infile.hasNext())
{
String word = infile.next();
outfile.println(word);
}
infile.close();
outfile.close();
System.out.println("Finished printing raw word list.");
}
catch (FileNotFoundException
e)
{
System.out.println("File does not exist. Goodbye.");
}
} // end of rawOutput method
// palindromeOutput method implementation
public static void palindromeOutput(File in, File
out)
{
try
{
Scanner infile =
new Scanner(in);
PrintWriter
outfile = new PrintWriter(out);
ArrayList<String> list = new ArrayList<String>();
while
(infile.hasNext())
{
String word = infile.next();
word = cleanup(word);
if (isPalindrome(word) &&
!list.contains(word))
{
list.add(word);
}
}
Collections.sort(list);
for(String word
: list)
{
outfile.println(word);
}
infile.close();
outfile.close();
System.out.println("Finished printing palindromes.");
}
catch (FileNotFoundException
e)
{
System.out.println("File does not exist. Goodbye.");
}
} // end of palindromeOutput method
// hundredDollarWordOutput method
implementation
public static void hundredDollarWordOutput(File in,
File out)
{
try
{
Scanner infile =
new Scanner(in);
PrintWriter
outfile = new PrintWriter(out);
ArrayList<String> list = new ArrayList<String>();
while
(infile.hasNext())
{
String word = infile.next();
word = cleanup(word);
if (wordValue(word) == 100 &&
!list.contains(word))
{
list.add(word);
}
}
Collections.sort(list);
for(String word
: list)
{
outfile.println(word);
}
infile.close();
outfile.close();
System.out.println("Finished printing $100 words.");
}
catch (FileNotFoundException
e)
{
System.out.println("File does not exist. Goodbye.");
}
} // end of hundredDollarWordOutput method
// isPalindrome method implementation
public static boolean isPalindrome(String word)
{
int i = 0;
int j = word.length() - 1;
while (i < j)
{
if
(word.charAt(i) != word.charAt(j))
{
return false;
}
i++;
j--;
}
return true;
} // end of isPalindrome method
// cleanup method implementation
public static String cleanup(String word)
{
String result = "";
for (char ch :
word.toCharArray())
{
if
(Character.isLetter(ch))
{
result += ch;
}
}
result = result.toUpperCase();
return result;
} // end of cleanup method
// cleanup method implementation
public static int wordValue(String word)
{
int value = 0;
for(int i = 0; i <
word.length(); i++)
{
value +=
((int)word.charAt(i) - 64);
}
return value;
} // end of cleanup method
} // end of StringParser class