Question:
I have problem with my Java Palindrome problem?
Alice L
2013-08-22 03:12:02 UTC
Solve this problem:
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.
Three answers:
AnalProgrammer
2013-08-22 04:07:21 UTC
You see the 41 here?

Palindrome.java:41)



Well that is the line number that is failing. Now look at the error.

IOException: Stream closed



You have closed both the input and the output.



So you logic is not right.

What you need is something like

Open the input and output.

line = inFile.readLine();



while ( ( line != null) {

System.out.println(line);



// Place some code here to do the palindrome check.

// Print the output information or

//Write to the output file.



line = inFile.readLine();

}



//Place any summary code here.



inFile.close();

//There should be no code here. This is the end of the program.



Have fun.
Gopi
2013-08-22 17:16:09 UTC
import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.util.logging.Level;

import java.util.logging.Logger;



public class Palindrome {



public static void main(String args[]) throws IOException {

// Reader to read the input file.

BufferedReader pals = null;

// Writer to write the output file.

BufferedWriter palsout = new BufferedWriter(new FileWriter("palsout.txt"));



try {

// Read the input file into the buffer.

pals = new BufferedReader(new FileReader("pals.txt"));

} catch (FileNotFoundException ex) {

System.out.println("'pals.txt' not found");

}





String line = "";

// For each line, get the string and verify palindrome or not.

// If the line read is NULL, it is the end of file and so done.

while ((line = pals.readLine()) != null) {

// Boolean to check palindrome validity.

boolean pal = true;

// Make a copy of the original line.

String string = line;

// Use Regex to replace non-letters with null character.

string = string.replaceAll("\\W", "");

// Convert all letters to lowercase to compare.

string = string.toLowerCase();

// Get the length of the line.

int length = string.length();

// Compare i-th letter to the last but i-th letter.

// e.g. 2nd letter to last but 2nd letter.

for (int i = 0; i < length / 2; i++) {

// If atleast one letter is not same, not a palindrome and so break;

if (string.charAt(i) != string.charAt(length - i - 1)) {

pal = false;

break;

}

}

// Write the result to the output file.

if (pal == true) {

palsout.write(line + " Is a palindrome.\n");

} else {

palsout.write(line + " Is not a palindrome.\n");

}

}

// Close all the files.

palsout.flush();

palsout.close();

pals.close();

}

}
Rick
2013-08-22 10:58:50 UTC
You close inFile here

PrintWriter outFile = new PrintWriter (new FileWriter("output.txt"));

outFile.println("The string and if is a palindrome");

inFile.close(); <============ inFile is closed here

outFile.close();





then you try to read from it again

while (another.equalsIgnoreCase("y")) // allows y or Y

{

str = inFile.readLine(); <===== read inFile here

left = 0;

right = str.length() - 1;



else

System.out.println ("Is a palindrome.");

System.out.println();

another = inFile.readLine(); <======= and here


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...