Question: Project#3 will exercise your skill at using a loop and String operations to determine if a given String is a palindrome or not. DO NOT

Project#3 will exercise your skill at using a loop and String operations to determine if a given String is a palindrome or not.

DO NOT WRITE ANY OUTPUT STATEMENTS ANYWHERE. I HAVE ALREADY WRITTEN ALL THE OUTPUT STATEMENTS.

You are given a program that is mostly written: Project3.java. It reads an input file: P3input.txt line by line using a while loop, a Scanner and a call to .nextLine(). Note that .nextLine() grabs the entire line, all the words with the embedded spaces and stores that line into a single string value.

Your job is to add your own loop inside my while loop. My while loop reads a line from the file and sets the isPalindrome flag to true. This means we start out assuming every line is a palindrome. The whole purpose of your loop is to determine whether the line really is a palindrome or not. If your loop detects any asymmetry it must put a false value into the isPalindrome flag. Once your loop has finished, my code will use whatever value is in the isPalindrome flag to know whether to print or not print the line. You are treat uppercase and lowercase letters as being identical, i.e. case does NOT matter.

The whole point of your loop is to either leave the true in the isPalindrome flag, or overwrite it with a false. My code will use that value to do the print (or not).

The best approach to testing whether a string is a palindrome is to write a loop that compares each char to the left of the middle against its mirror char on the right of the middle. Repeat your testing until you find a mismatch. As soon you do find a mismatch set the isPalindrome flag to false. You can even break out of your loop because once you have put a false into isPalindrome there is no need to test any more chars for asymmetry. If your loop ends without ever having found a mismatch then the isPalindrome flag will still have the true in it, and the line is a palindrome.

/* CS 007 Project3.java

Reads through file line by line. Prints only those lines that are palindromes

*/

import java.io.*;

import java.util.*;

public class Project3

{

public static void main (String args[]) throws Exception // throws error if input file can't be found/opened

{

// ALWAYS TEST FIRST TO VERIFY USER PUT REQUIRED INPUT FILE NAME ON THE COMMAND LINE

if (args.length

{

System.out.println(" usage: C:\\> java Project3 "); // i.e. C:\> java Project3 P3input.txt

System.exit(0);

}

Scanner infile = new Scanner( new File(args[0]) );

while( infile.hasNextLine() )

{

String line = infile.nextLine();

boolean isPalindrome=true; // each line initially assumed palindrome

// ---------- DONT WRITE ANYTHING ABOVE THIS LINE -------------------

/* A L L Y O U R C O D E G O E S H E R E */

// ----------- DONT WRITE ANYTHING BELOW THIS LINE -------------------

if ( isPalindrome )

System.out.println( line ); // only print if its a a palindrome

} // END WHILE FILE HAS ANOTHER LINE IN IT

infile.close();

} // END MAIN

} // END PROJECT3 CLASS

P3input.tt - Notepad File Edit Format View Help Madam I mAdam Stanley Yelnats a man a plan a canal panama a can a panasan can wow radar wow palindromic cimordnilap this is no plaindrome

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!