Alice L
2013-08-22 03:12:02 UTC
A palindrome is a string of characters that reads the same forward and backward. Write a program that reads in strings of characters and determines if each string is a palindrome. Each string appears on a separate input line. Echo-print each string, followed by "Is a palindrome" if the string is a palindrome or "Is not a palindrome" if the string is not a palindrome. For example, given the input string
Able was I, ere I saw Elba.
The program should print "Is a palindrome." In determining whether a string is a palindrome, consider uppercase and lowercase letters to be the same and ignore punctuation characters and spaces.
Notes on this problem:
the input file that you will be reading palindromes from is called “pals.txt”.
make sure to check out the documentations of the class String from the website pointed to by your book as you will need some more string manipulations than the ones you are already familiar with (look for methods like replace(), toLowerCase, toUpperCase..etc) to do this problem.
write your output (the string and if is a palindrome) to a file called “palsout.txt”.
each line of text in the file pals.txt will contain a palindrome to test for.
You are not allowed to use the Scanner class for this problem. Any attempts to use the Scanner class would result in taking points off. The BufferedReader class should be used instead. An example of BufferedReader class was shown to you in chapter 3.
The palindrome file that contains all the palindromes that I will test your program with is on our CIE website at http://cie-wc.edu/student_downloads.aspx. Make sure you use it to test your program.
1. program has to compile with no errors.
2. program has to produce the right output
3. programs must have your name, student number and assignment number placed in the code as comments, no exceptions!
4. program has to have all the specifications asked in the problem
5. Also, comments, useful identifiers, and good programming practices
Here is my code
import java.io.*;
public class Palindrome
{
public static void main (String [] args) throws IOException
{
BufferedReader inFile = new BufferedReader (new FileReader ("pals.txt"));
String line;
line = inFile.readLine(); //initialize
String str, another = "y";
int left, right;
String newString = "";
while(line != null) // boolean expression
{
System.out.println(line);
line = inFile.readLine(); //read the next line
}
PrintWriter outFile = new PrintWriter (new FileWriter("output.txt"));
outFile.println("The string and if is a palindrome");
inFile.close();
outFile.close();
while (another.equalsIgnoreCase("y")) // allows y or Y
{
str = inFile.readLine();
left = 0;
right = str.length() - 1;
while (str.charAt(left) == str.charAt(right) && left < right)
{
left++;
right--;
}
System.out.println();
if (left < right)
System.out.println ("Is NOT a palindrome.");
else
System.out.println ("Is a palindrome.");
System.out.println();
another = inFile.readLine();
}
}
}
I keep getting the
--------------------Configuration:
able was I, I saw elba.
A man, A plan, A canal-panama!
madam, in eden I'm adam.
Radar.
Palindrome
Exception in thread "main" java.io.IOException: Stream closed
at java.io.BufferedReader.ensureOpen(BufferedReader.java:97)
at java.io.BufferedReader.readLine(BufferedReader.java:292)
at java.io.BufferedReader.readLine(BufferedReader.java:362)
at Palindrome.main(Palindrome.java:41)
Process completed.
I can not figure it out where did I go wrong. Please help! Thank you so much.