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

HELP IN JAVA: Write a program to read several lines from a file. For each line,

ID: 3717743 • Letter: H

Question

HELP IN JAVA:Write a program to read several lines from a file. For each line, check whether it is a palindrome or nearly a palindrome. For each line read from the file, print only the lines that are palindromes or nearly palindromes. Recall that a palindrome is a number that is the same backwards and forwards. Some examples of palindromes are 14641 and 22. A number that is nearly a palindrome is a number where if you changed 1 digit, it would be a palindrome. Some examples are 14671 and 32. Your palindrome checking method must be recursive.

You need a recursive method that checks to see if a number is a palindrome or nearly a palindrome. Some hints:

1. This is actually easiest if you convert the number to a string and then check the characters of the string.

2. You need to check the characters of the string from the left and from the right simultaneously to see if they match.

3. The check is allowed 1 mismatch, but only 1—no more.

4. You can keep track of everything in parameters in the recursive method: the string, the left position, the right position, and the number of mismatches found so far. So the recursive method header looks like this: boolean detectPalindrome(String s, int left, int right, int mismatches)

5. When you call the method the first time, there are 0 mismatches, left starts at position 0, and right at the last character of the string. So the initial method call looks like this: detectPalindrome(s, 0, s.length()-1, 0)

6. Now, there are two stopping cases (base cases): The first is when the check hits two mismatches. At that point you know the number is not a palindrome or nearly a palindrome, so you can return false: if (mismatches==2) return false;

7. The other stopping case is when the check hits the middle of string and there are less than 2 mismatches. The middle of the string is either when left and right hit the single digit in the middle (for an odd-length string) or they cross over each other because all the digits have been checked (for an even-length string). You can check this with one expression like this: if (right-left <= 0) return true;

8. Finally, there are two recursive cases: one where there is a match and one where there is a mismatch. In the second case, you have to add 1 to the number of mismatches in the recursive call. In both cases you have to add 1 to the left position and subtract one from the right position: if (s.charAt(left)==s.charAt(right)) return detectPalindrome(s, left+1, right-1, mismatches); else return detectPalindrome(s, left+1, right-1, mismatches+1);

9. That is all you need for the recursive method. The last thing to do is combine this with reading numbers from the file.

list.txt:

6171

81513111

316111

69396

6101

122221

313161

61514161

11310161

7161

61813111

61212111

31013171

312101

41313121

61515151

811141

2151

56365

717101

5151

12

3171

Explanation / Answer

Given below is the code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you
NOTE: Please make sure you place your input file in the correct folder. If using eclipse, the file should be in the project directly and NOT INSIDE src folder.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class DetectPalindrome {
public static boolean detectPalindrome(String s, int left, int right, int mismatches)
{
if(mismatches == 2)
return false;
else if (right-left <= 0)
return true;

if(s.charAt(left)==s.charAt(right))
return detectPalindrome(s, left+1, right-1, mismatches);
else
return detectPalindrome(s, left+1, right-1, mismatches+1);
}

public static void main(String[] args) {
String filename;
Scanner keyboard = new Scanner(System.in);

System.out.println("Enter the input filename: ");
filename = keyboard.nextLine();

try {
Scanner infile = new Scanner(new File(filename));

while(infile.hasNextLine())
{
String line = infile.nextLine();
if(detectPalindrome(line, 0, line.length()-1, 0))
System.out.println(line + " => Palindrome/Near Palindrome");
else
System.out.println(line + " => NOT Palindrome");
}
infile.close();
} catch (FileNotFoundException e) {

System.out.println(e.getMessage());
}

}
}

list.txt
--------
6171
81513111
316111
69396
6101
122221
313161
61514161
11310161
7161
61813111
61212111
31013171
312101
41313121
61515151
811141
2151
56365
717101
5151
12
3171
14671

output
--------
Enter the input filename:
list.txt
6171 => NOT Palindrome
81513111 => NOT Palindrome
316111 => NOT Palindrome
69396 => Palindrome/Near Palindrome
6101 => NOT Palindrome
122221 => Palindrome/Near Palindrome
313161 => NOT Palindrome
61514161 => NOT Palindrome
11310161 => NOT Palindrome
7161 => NOT Palindrome
61813111 => NOT Palindrome
61212111 => NOT Palindrome
31013171 => NOT Palindrome
312101 => NOT Palindrome
41313121 => NOT Palindrome
61515151 => NOT Palindrome
811141 => NOT Palindrome
2151 => NOT Palindrome
56365 => Palindrome/Near Palindrome
717101 => NOT Palindrome
5151 => NOT Palindrome
12 => Palindrome/Near Palindrome
3171 => NOT Palindrome
14671 => Palindrome/Near Palindrome