Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In Java For this assignment do not worry about error checking any of the the inp

ID: 3847447 • Letter: I

Question

In Java

For this assignment do not worry about error checking any of the the inputs. As usual, all displayed prompts and results should be well formatted

This program will identify palindrome and non-palindrome words in a text file named words.txt. The programmer will create the file with each line having one word.
The words in the file may or may not be palindromes.

Use google to look up palindromes to put in the file and also include non-palindromes in the file. Upper and lower case characters are considered to be a match for palindrome analysis.
This program must use the Java String Classes and related classes.
Console display control must be well formatted.

The program should display the result using the original word in a statement that indicates if it is a palindrome or not, such as:

Dad is a palindrome.
Apple is not a palindrome.

When the file has been processed, the program should display basic statistics on how many words were processed, how many were palindromes and how many were not palindromes.

Explanation / Answer

FileStringPalindrome.java

import java.io.File;
import java.io.FileNotFoundException;


public class FileStringPalindrome {

   public static void main(String[] args) throws FileNotFoundException {
       String fileName = "D:\words.txt";
       File file = new File(fileName);
       java.util.Scanner in = new java.util.Scanner(file);
       int totalWordCount = 0, palinCount =0,nonPalinCount = 0;
       while(in.hasNextLine()){
       String str = in.nextLine();
       boolean status = isPalindrome(str);
       totalWordCount++;
       if(status){
           palinCount++;
           System.out.println(str+" is a palindrome");
       }
       else{
           nonPalinCount++;
           System.out.println(str+" is not a palindrome");
       }
       }
       System.out.println("-------------------------------------");
       System.out.println("Number of words were processed: "+totalWordCount);
       System.out.println("Number of palindromes: "+palinCount);
       System.out.println("Number of non-palindromes: "+nonPalinCount);
   }
   public static boolean isPalindrome(String str){
       // if length is 0 or 1 then String is palindrome
if(str.length() == 0 || str.length() == 1)
return true;
str = str.toLowerCase();
for(int i=0; i<str.length()/2; i++){
   /*checking each character from front and back both are same or not. if not, strign is not palindrome*/
if(str.charAt(i) != str.charAt(str.length()-1-i))
{
   return false;
}
}
/*if all characters from front anf nack then it is palindrome string.*/
return true;
   }
}

Output:

Dad is a palindrome
Apple is not a palindrome
Madam is a palindrome
Sir is not a palindrome
Hai is not a palindrome
-------------------------------------
Number of words were processed: 5
Number of palindromes: 2
Number of non-palindromes: 3

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote