Description
Find and print out all the palindromes in a file Java Program
Find and print out all the palindromes in a file, ignoring any strings with numbers in them, and printing them out in alphabetical order. Your program should:
1. Open the file, dealing with possible exceptions
If there is no file name given, print “insufficient arguments given”
If there is no file, print “file not found”
2. Read the file word by word, where words are any strings of characters separated by whitespace. For example “ghoud3421” is a word, but as it contains digits you would not test to see if it is a palindrome.
3. For each word, use a separate method to see if it contains digits, and if it does not, check using another method whether it is a palindrome.
4. Collect all the palindromes, including duplicates.
5. Print out all the palindromes in alphabetical order.
A palindrome is a word whose meaning may be interpreted the same way in either forward or reverse direction. e.g. dad, tattarrattat, abccba, abbba, aba.
Learning Outcomes
File I/O
String manipulation
Using the Java Collections
Sample Input/Output
Case A
Input: java PalindromeCounter
Output: insufficient arguments given
Case B
Input: java PalindromeCounter nofile
Output: file not found
Case C
Input: java PalindromeCounter file1.txt
file1.txt:
abcd a1a aa1 a
ccc
a !#! z#z %1%
haah Zaz
Output:
!#!
a
a
ccc
haah
z#z
Skeleton Code
textpop-up
public classPalindromeCounter {
public static void main(String[] args) {
// TODO fill in this method
}
protected static boolean isValidWord(Stringword) {
// TODO fill in this method
}
protected static boolean isPalindrome(Stringword) {
// TODO fill in this method
}
}
Reviews
There are no reviews yet.