Question: In Project#3 you read in lines from a file and tested each to see if it was a palindrome or not. In this Lab you
In Project#3 you read in lines from a file and tested each to see if it was a palindrome or not. In this Lab you will take your working solution from that assignment and transplant your code into a boolean method named isPalindrome(). As usual you are given a starter file with the main written. This time you will add your code inside the isPalindrome methods that is just an empty shell.
The expected output for Lab#4 is IDENTICAL to what Project#3 produced. The only difference is that your main program will call your boolean method isPalindrome() to determine whether the line is to be printed or not.
// Lab4.java Starter File Uses a method to print only palindromes from a text file
import java.io.*; // BufferedReader
import java.util.*; // Scanner
public class Lab4
{
public static void main (String args[]) throws Exception // i.e. the input file you put on cmd line is not in directory
{
// ALWAYS TEST FIRST TO VERIFY USER PUT REQUIRED INPUT FILE NAME ON THE COMMAND LINE
if (args.length
{
System.out.println(" usage: C:\\> java Lab4 "); // i.e. C:\> java Lab4 L4input.txt
System.exit(0);
}
Scanner infile = new Scanner( new File(args[0]) ); // we read our text file line by line
while( infile.hasNextLine() )
{
String line = infile.nextLine();
if ( isPalindrome(line) )
System.out.println(line);
}
infile.close();
} // END MAIN
// RETURNs true if and only if the string passed in is a palindrome
static boolean isPalindrome( String line )
{
CODE GOES HERE
return false; // Just to make it compile. You change as needed
}
} // END LAB4 CLASS
Command Prompt C: \Users\tim\Desktop\lab-04 solution>java Lab4 L4input.txt Madam I mAdam Stanley Yelnats wow radar wow palindromic cimordnilap C: \Users\tim\Desktop\lab-04\solution
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
