JAVA Program: Input and Output Write a JAVA program that takes an input file as
ID: 3795594 • Letter: J
Question
JAVA Program: Input and Output
Write a JAVA program that takes an input file as an argument and writes to an output file with the following properties:
-Read input file and search for palindromes (Words spelled the same forward and backward, such as racecar)
------public static boolean isPalindrome (String word)
-Output file displays:
------Number of palindromes found on first line
------Each palindrome found on new line
-Make sure to divide the program into logical methods
-Make sure to handle errors and exceptions
-Make sure to use default package.
Explanation / Answer
Please use the below two classes as a solution.
PalindromExecutor.java :
import java.util.Scanner;
public class PalindromExecutor extends PalindromFuncrions{
public PalindromFuncrions PFS = new PalindromFuncrions();
public static void main(String[] args) {
// TODO Auto-generated method stub
String FP;
Scanner in = new Scanner(System.in);
FP=in.nextLine();
//Calling the palindrom functions
PalindromCount(getWords(FP));
System.out.println("The number of palindromes are : "+palindromCount);
}
}
PalindromFuncrions.java :
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
public class PalindromFuncrions {
public static String[] words;
public static int palindromCount=0;
public static String[] getWords(String FilePath){
if (FilePath == null) {
return null;
}
FilePath=FilePath.replaceAll("\", "/");
//ArrayList<String> list = new ArrayList<String>();
try {
FileInputStream fin=new FileInputStream(FilePath);
BufferedReader br=new BufferedReader(new InputStreamReader(fin));
String Line;
try {
while((Line=br.readLine())!=null)
{
words = Line.split(" ");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("An error occured while reading a file : " + " ");
e.printStackTrace();
}
return words;
}
public static int PalindromCount(String[] words){
for(int i=0; i < words.length; i++){
String word = words[i].toLowerCase().toString();
if(!isPalindrom(word)){
System.out.println("The word "+word+" is not a palindrom");
}else{
palindromCount++;
//return palindromCount;
}
}
return palindromCount;
}
public static boolean isPalindrom(String word){
String tempStr=word;
String revStr="";
for(int i=tempStr.length()-1;i>0;--i){
revStr +=tempStr.charAt(i);
}
if(!revStr.equals("") && revStr.equalsIgnoreCase(word)){
return true;
}
return false;
}
}
import java.util.Scanner;
public class PalindromExecutor extends PalindromFuncrions{
public PalindromFuncrions PFS = new PalindromFuncrions();
public static void main(String[] args) {
// TODO Auto-generated method stub
String FP;
Scanner in = new Scanner(System.in);
FP=in.nextLine();
//Calling the palindrom functions
PalindromCount(getWords(FP));
System.out.println("The number of palindromes are : "+palindromCount);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.