Question

In: Computer Science

Write a Java program that will test lines of text to see if they are palindromes....

Write a Java program that will test lines of text to see if they are palindromes. The program should prompt the user for the name of a file to process, then open and read the file of possible palindromes (one per line of text). The program should then print the total number of lines read, and total number of palindromes.

Solutions

Expert Solution

======

import java.io.BufferedReader;

import java.io.File;

import java.io.FileReader;

import java.io.IOException;

import java.io.InputStreamReader;

public class Main {

public static void main(String args[]) {

try {

BufferedReader brRead = new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter file path:");

String filePath = brRead.readLine();

int palCount = 0, lines = 0;

File file = new File(filePath);

FileReader fr = new FileReader(file); // reads the file

BufferedReader br = new BufferedReader(fr);

String line;

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

boolean pal = isPallindrome(line);

if(pal == true)

palCount++;//count pallindorme

lines++;//count number of lines.

}

fr.close();

System.out.println("Number of Pallindromes: " + palCount);

System.out.println("Number of Lines Read: " + lines);

} catch (IOException e) {

e.printStackTrace();

}

}

public static boolean isPallindrome(String str) {

StringBuilder sb = new StringBuilder(str);

sb.reverse();

String rev = sb.toString();

if (str.equals(rev)) {

return true;

} else {

return false;

}

}

}

Input

=====

My name is Pill

Tom isi moT

Harry say yas yrraH

Rosie is blue

Output:

======

Enter file path:E://Demo.txt

Number of Pallindromes: 2

Number of Lines Read: 4

Screenshot for reference:

====================


Related Solutions

Write the program in java Write a program that does basic encrypting of text. You will...
Write the program in java Write a program that does basic encrypting of text. You will ask the user the filename of a text file which contains a few sentences of text. You will read this text file one character at a time, and change each letter to another one and write out to an output file. The conversion should be done a -> b, b->c , … z->a, A->B, B->C, …. Z->A. This means just shift each letter by...
Write Java program Lab52.java which reads in a line of text from the user. The text...
Write Java program Lab52.java which reads in a line of text from the user. The text should be passed into the method: public static String[] divideText(String input) The "divideText" method returns an array of 2 Strings, one with the even number characters in the original string and one with the odd number characters from the original string. The program should then print out the returned strings.
Write a program that allows the user to navigate the lines of text in a file....
Write a program that allows the user to navigate the lines of text in a file. The program should prompt the user for a filename and input the lines of text into a list. The program then enters a loop in which it prints the number of lines in the file and prompts the user for a line number. Actual line numbers range from 1 to the number of lines in the file. If the input is 0, the program...
Write a program that allows the user to navigate the lines of text in a file....
Write a program that allows the user to navigate the lines of text in a file. The program should prompt the user for a filename and input the lines of text into a list. The program then enters a loop in which it prints the number of lines in the file and prompts the user for a line number. Actual line numbers range from 1 to the number of lines in the file. If the input is 0, the program...
Write a Java program that will encode plain text into cipher text and decode cipher text...
Write a Java program that will encode plain text into cipher text and decode cipher text into plain text. Create following methods and add them to your class: • a method named encode(String p, int key) that takes a plain text p and encodes it to a cipher text by adding the key value to each alphabet character of plain text p using the ASCII chart. The method should return the encoded String. For example, if p = "attack at...
Please write a java program to write to a text file and to read from a...
Please write a java program to write to a text file and to read from a text file.
The JAVA program should do the following: –Ask the user for how many lines of text...
The JAVA program should do the following: –Ask the user for how many lines of text they wish to enter –Declare and initialize an array of Strings to hold the user’s input –Use a while loop to prompt for and read the Strings (lines of text) from the user at the command line. Typically, this would be a 'for' loop since we know the number of times to execute the loop based upon the number supplied by the user, but...
URGENT!! DO THIS CODE IN JAVA Write a complete Java FX program that displays a text...
URGENT!! DO THIS CODE IN JAVA Write a complete Java FX program that displays a text label and a button.When the program begins, the label displays a 0. Then each time the button is clicked, the number increases its value by 1; that is each time the user clicks the button the label displays 1,2,3,4............and so on.
Write a JAVA program that compares two strings input to see if they are the same?
Write a JAVA program that compares two strings input to see if they are the same?
Write java program that will ask for the user for 2 input lines and print out...
Write java program that will ask for the user for 2 input lines and print out all words that occur 1 or more times on both lines (case sensitive). Write this without arrays and method. Here is a sample run: <Output> Enter two lines to process. The quick brown fox jumps over a lazy dog The fox hound outruns the lazy dog The words that occur on both lines are: The fox lazy dog
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT