Please work on this some more. I cannot pass you as is. I am just
having trouble following what you have sent in.
import java.io.*;// import for using BufferedReader, BufferedWriter
public class Palindrome
{
public static void main( String args[] ) throws IOException
{
try
{
BufferedReader inputStream = new BufferedReader(new FileReader("pals.txt"));
BufferedWriter outputStream = new BufferedWriter(new FileWriter("palsout.txt"));// using BufferedWriter instead of PrintWriter
String line;
String lineCopy;
line = inputStream.readLine();// starts reading file by the line
while( line != null )
{
// strip punctuation, spaces, and special characters, then convert to lower case
String stripArray[] = { "\"", "'", ".", " ", ",", ";", ":", "!", "@", "#", "$", "%", "^", "&", "\\", "(", ")", "_", "-", "+", "=", "?", "[", "]", "{", "}", "<", ">", "/" };
lineCopy = line;
for(String charReplace:stripArray)// remove each element in stripArray from lineCopy
{
lineCopy = lineCopy.replace( charReplace,"" );
}
lineCopy = lineCopy.toLowerCase();
StringBuilder copyString = new StringBuilder(lineCopy);
copyString.reverse();// reverse the character order in copyString
if( lineCopy.equals(copyString.toString()) )// it's a palindrome
{
System.out.print( line + " Is a palindrome.\n" );
outputStream.write( line + " Is a palindrome." );
outputStream.newLine();
}
else// it's not a palindrome
{
System.out.print( line + " Is not a palindrome.\n" );
outputStream.write( line + " Is not a palindrome." );
outputStream.newLine();
}
line = inputStream.readLine();// read the next line
}
inputStream.close();
outputStream.close();
}
catch( java.io.FileNotFoundException e )
{
System.out.print("The file \"pals.txt\" must be located in the same directory as this application.");
}
}
}
3 Comments:
Did you ever get this one figured out? I am working on it right now...
Did you ever get this one to work the way they wanted it to?
The code works as is. It isn't problematic; the code was not being compiled by the party evaluating the work.
I did attempt to explain throwing exceptions, which probably added to the confusion.
But the code functions as a palindrome checker. Not the only way to do it, and probably not the best way, but it does work.
Post a Comment
<< Home