HELP IN JAVA: *NOTE: contents of input file will put in the bottom Write a progr
ID: 3714283 • Letter: H
Question
HELP IN JAVA: *NOTE: contents of input file will put in the bottom
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.
*palinTest.txt*
6171
81513111
316111
69396
6101
122221
313161
61514161
11310161
7161
61813111
61212111
31013171
312101
41313121
61515151
811141
2151
56365
717101
5151
12
3171
213131
51515131
33
2141
611171
384463
61813181
8131
21814141
66
11612151
114131
71214181
71511101
318101
81110131
96
514181
99
41818141
11016151
61510151
58382
25352
616121
71414121
5141
418141
21516111
61813131
8141
5131
71817161
512131
81117131
115101
238232
416181
61518131
23332
413101
5121
117131
81116111
24442
21810121
115161
7161
311171
21115131
81118101
31317131
6151
2161
31811141
198891
218111
21017151
816151
81415121
711101
816151
4181
21312
11216181
3141
118141
56375
81211131
41515141
19
4101
49394
61016111
614171
41364
61514121
414161
216111
538838
617181
31311131
939969
8131
110161
3121
2101
6121
Explanation / Answer
PROGRAM
import java.io.*;
// Create PalTest class
class PalTest
{
// create and implement static method detectPalindrome() method
public static boolean detectPalindrome(String s,int left,int right,int mismatches)
{
if(mismatches==2) return false; // check condition mismatches=2 then return false
if(mismatches<2){ // check condition mismatches<2 then right-left<=0 then return true
if ((right-left) <= 0) return true;
}
if(s.charAt(left)==s.charAt(right)) // check condition left digit and right digits are equal then
return detectPalindrome(s,left+1,right-1,mismatches); // call recursively the parameter mismatches=0
else
return detectPalindrome(s,left+1,right-1,mismatches+1); // else call recursively the add 1 to parameter mismatches
}
public static void main(String [] args) throws IOException
{
try // open try..catch block to check whether the file is available or not
{
File f=new File("F:\palinTest.txt"); // create object f for File
BufferedReader b=new BufferedReader(new FileReader(f)); // create object b of BufferedReader
String str=""; // initialized String str null
while((str=b.readLine())!=null){ // create while loop until end-of-file
if(detectPalindrome(str,0,str.length()-1,0)) // calling detectPalindrome and check condition then display palindrome else not palindrome
System.out.println("Palindrome= "+str);
else
System.out.println("Not Palindrom= "+str);
}
}catch(IOException e) // catch, when the file is not found, then display IOException
{
e.printStackTrace();
}
}
}
OUTPUT
F:>javac PalTest.java
F:>java PalTest
Not Palindrom= 6171
Not Palindrom= 81513111
Not Palindrom= 316111
Palindrome= 69396
Not Palindrom= 6101
Palindrome= 122221
Not Palindrom= 313161
Not Palindrom= 61514161
Not Palindrom= 11310161
Not Palindrom= 7161
Not Palindrom= 61813111
Not Palindrom= 61212111
Not Palindrom= 31013171
Not Palindrom= 312101
Not Palindrom= 41313121
Not Palindrom= 61515151
Not Palindrom= 811141
Not Palindrom= 2151
Palindrome= 56365
Not Palindrom= 717101
Not Palindrom= 5151
Palindrome= 12
Not Palindrom= 3171
Not Palindrom= 213131
Not Palindrom= 51515131
Palindrome= 33
Not Palindrom= 2141
Not Palindrom= 611171
Palindrome= 384463
Not Palindrom= 61813181
Not Palindrom= 8131
Not Palindrom= 21814141
Palindrome= 66
Not Palindrom= 11612151
Not Palindrom= 114131
Not Palindrom= 71214181
Not Palindrom= 71511101
Not Palindrom= 318101
Not Palindrom= 81110131
Palindrome= 96
Not Palindrom= 514181
Palindrome= 99
Not Palindrom= 41818141
Not Palindrom= 11016151
Not Palindrom= 61510151
Palindrome= 58382
Palindrome= 25352
Not Palindrom= 616121
Not Palindrom= 71414121
Not Palindrom= 5141
Not Palindrom= 418141
Not Palindrom= 21516111
Not Palindrom= 61813131
Not Palindrom= 8141
Not Palindrom= 5131
Not Palindrom= 71817161
Not Palindrom= 512131
Not Palindrom= 81117131
Not Palindrom= 115101
Palindrome= 238232
Not Palindrom= 416181
Not Palindrom= 61518131
Palindrome= 23332
Not Palindrom= 413101
Not Palindrom= 5121
Not Palindrom= 117131
Not Palindrom= 81116111
Palindrome= 24442
Not Palindrom= 21810121
Not Palindrom= 115161
Not Palindrom= 7161
Not Palindrom= 311171
Not Palindrom= 21115131
Not Palindrom= 81118101
Not Palindrom= 31317131
Not Palindrom= 6151
Not Palindrom= 2161
Not Palindrom= 31811141
Palindrome= 198891
Not Palindrom= 218111
Not Palindrom= 21017151
Not Palindrom= 816151
Not Palindrom= 81415121
Not Palindrom= 711101
Not Palindrom= 816151
Not Palindrom= 4181
Palindrome= 21312
Not Palindrom= 11216181
Not Palindrom= 3141
Not Palindrom= 118141
Palindrome= 56375
Not Palindrom= 81211131
Not Palindrom= 41515141
Palindrome= 19
Not Palindrom= 4101
Palindrome= 49394
Not Palindrom= 61016111
Palindrome= 14171
Not Palindrom= 1364
Palindrome= 1514121
Palindrome= 14161
Not Palindrom= 216111
Palindrome= 538838
Not Palindrom= 617181
Not Palindrom= 31311131
Palindrome= 939969
Not Palindrom= 8131
Not Palindrom= 110161
Not Palindrom= 3121
Not Palindrom= 2101
Not Palindrom= 6121
Palindrome= 14671
Palindrome= 32
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.