Computer Science Homework Solutions
Problem
#137206

Java: Palindrome Using BufferedReader

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 stringis not a palindrome.  For example, given the input string Able was, I ere I saw Elba.  The program should print "Is a palindrome."  In detemining whether a string is a palindrome, consider uppercase and lower case letters to be the same and ignore punctuation characters and spares.  The input file that you will be reading palindromes from is called "pals.txt".   Look for methods like replace (), to lowerCase, to UpperCase,..etc.  Write the 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 can only use the BufferedReader class and not the Scanner class.  Please examine the attachment, this is as far as I got, however I am able to compile but when executing I get and error.  Please select palindrome files to save in the pals.txt file.

Attached file(s):
Attachments
Palindrome Test.doc  View File

Attachment Content Summary (Note: view attachment at the above link before purchasing. Actual attachment content may vary slightly from that shown below.)

Palindrome Test.doc
import java.io.*;

public class PalindromeTester

{

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();



}



}

}
Solution
What is this?
By OTA - Overall OTA Rating
Purchase Cost Now
$2.19 CAD (was ~$7.98)
Included in Download
  • Plain text response
  • Attached file(s):
    • PalindromeTester.java
$2.19 Instant Download
Add to Cart
Why you can trust BrainMass.com
  • Your Information is Secure
  • Best Online Academic Help Service
  • Students find real academic Success
Related Solutions
Browse