Java Program that reads a given file (input file) and: Determines the total numb
ID: 3715719 • Letter: J
Question
Java Program that reads a given file (input file) and:
Determines the total number of palindromes in the input file (a word that reads the same backward as forward such as kayak)
A palindrome is made of only alphabetical symbols. Therefore "1a1" is not considered a palindrome.
Generate the iinput file to test the program, however you do not need to include the input file in your final submission folder
Writes all the words that begin with a to m into one file(output1.txt) and all the remaining words into another file (output2.txt)
there should be no duplicates in output1 and output2
Two output files should be sorted in acending order
The inputfile is not necessarily sorted and may include duplicates
The word exam goes to output1.txt, the word "1a1" goes into output2.txt
Explanation / Answer
//*************Code*********
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class Palindrome {
public static boolean isAlphabate(String word) {
char[] chars = word.toCharArray();
for (char c : chars) {
if(!Character.isLetter(c)) {
return false;
}
}
return true;
}
public static boolean isPalindrome(String word)
{
// reverse the given String
String reverse = new StringBuffer(word).reverse().toString();
// check whether the string is palindrome or not
if (word.equals(reverse))
return true;
return false;
}
public static void writeIntoFile(Set output, String fileName)
{
//sort the output
List list = new ArrayList(output);
Collections.sort(list);
PrintWriter writer;
try {
writer = new PrintWriter(fileName, "UTF-8");
for(int i=0;i<list.size();i++)
{
writer.println(list.get(i));
}
writer.close();
} catch (FileNotFoundException | UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(fileName +" is generated . Please check in current working folder.");
}
public static void main(String [] args)
{
//declare variables
String line;
int totalPalindrome=0;
//declare set variable to that it doesn't containt duplicates
Set output1 = new HashSet();
;
Set output2=new HashSet();
try (
//Read input file
InputStream fis = new FileInputStream("input.txt");
InputStreamReader isr = new InputStreamReader(fis, Charset.forName("UTF-8"));
BufferedReader br = new BufferedReader(isr);
) {
//read line by line
while ((line = br.readLine()) != null) {
// Do your thing with line
String[] words = line.split(" ");
for(int i=0;i<words.length;i++)
{
//if word is alphabate and is palindrome then increment count by 1
if(isAlphabate(words[i]) && isPalindrome(words[i]))
{
totalPalindrome++;
}
//check if word starts with from a to m
if(words[i].startsWith("a") || words[i].startsWith("b") ||
words[i].startsWith("c") || words[i].startsWith("d") ||
words[i].startsWith("e") || words[i].startsWith("f") ||
words[i].startsWith("g") || words[i].startsWith("h") ||
words[i].startsWith("i") || words[i].startsWith("j") ||
words[i].startsWith("k") || words[i].startsWith("l") ||
words[i].startsWith("m")
)
{
output1.add(new String(words[i]));
}
else
{
output2.add(new String(words[i]));
}
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//Print total palindromes
System.out.println("Total palindromes are: "+ totalPalindrome);
//Call functions to sort and wrtite into respective files
writeIntoFile(output1, "output1.txt");
writeIntoFile(output2, "output2.txt");
}
}
//***********Intput.txt***********
I am abhay kumar. I live in India.Kayak seems nice things 1a1 is not a palindrome. Is hooh is palindrome?
Palindrome are like 123321 and taat . mom and also dad is also palindrome.
//*********output1.txt*******
a
abhay
also
am
and
are
dad
hooh
in
is
kumar.
like
live
mom
//*********output2.txt*************
.
123321
I
India.Kayak
Is
Palindrome
nice
not
palindrome.
palindrome?
seems
taat
things.
1a1
//*******Please do let me know if you have any doubts or want me to modify the code*****
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.