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

It is work but when I typed 1245324, It says it is Palindrome. How to fix? impor

ID: 3737696 • Letter: I

Question

It is work but when I typed 1245324, It says it is Palindrome. How to fix?

import java.util.Stack;

import java.util.Scanner;

import java.util.Queue;

import java.util.LinkedList;

public class Palindrome {

// This is a main method to test checkPalindrome method

public static void main(String[] args) {

String inputString;

Scanner in = new Scanner(System.in);

do {

// please put your code to test checkPalindrome method here

System.out.println("please enter any string ");

inputString=in.nextLine();

int res=checkPalindrome(inputString);

if(res==0){

System.out.println("string is a palindrome");

}

else{

System.out.println("not a palindrome");

}

System.out.println("do you want to continue(y/n)");

inputString=in.nextLine().toLowerCase();

} while (inputString.equals("y") && inputString.length() == 1 );

System.out.print("Bye!");

}

// This is checkPalindrome method. It checks if an input string is Palindrome or not.

// It returns 0 if a string is a Palindrome. Otherwise, it returns a position of a character where it finds

// a different value.

// Pre-Condition: string must not be null.

// Post-Condition: Return 0 if input string is a Palindrome. Return a positive number indicate the location where

// a difference found.

public static int checkPalindrome(String strValue) {

Stack<Character> stack = new Stack<Character>();

Queue<Character> queue = new LinkedList<Character>();

int indexVal=-1;

// check if string is null. If it is null, return a -1

if(strValue!=null && strValue!=""){

// normalize the string values to lower case, remove spaces

strValue = strValue.toLowerCase().replaceAll("\W", "");

for(int i=0;i<strValue.length();i++){

stack.push(strValue.charAt(i));

queue.add(strValue.charAt(i));

}

while(!stack.isEmpty()){

String c=queue.remove().toString();

String d=stack.pop().toString();

if(!d.equals(c)){ //checks if each and every character is equal

indexVal=strValue.indexOf(c);

break;

}

else{

indexVal=0;

}

}

}

else{

indexVal=-1;;

}

// store data on stack/queue adts first

// loop: comparing, retrieving text, terminate loop if stack is emptied or found a difference

return indexVal;

}

}
-----------------------------------------------------------
** Unit test cases for checking the Palindrome method **/

import org.junit.Assert;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;

public class PalindromeTest {

/** Fixture initialization (common initialization
* for all tests). **/
String testStr = new String("");
@Before public void setUp() {
testStr = "";

}

/** Step on no pets test. **/
@Test public void defaultTest() {
testStr = "Step on no pets";
int check = Palindrome.checkPalindrome(testStr);
assertEquals(0, check);
}

/** null test. **/
@Test public void nullTest() {
testStr = null;
int check = Palindrome.checkPalindrome(testStr);
assertEquals(-1, check);
}

/** BelphegorPrime number test. **/
@Test public void BelphegorPrimeTest() {
testStr = "1000000000000066600000000000001";
int check = Palindrome.checkPalindrome(testStr);
assertEquals(0, check);
}

/** fail lonely tylenol test. **/
@Test public void failTest() {
testStr = "Lonely Tvlenol";
int check = Palindrome.checkPalindrome(testStr);
assertEquals(6, check);
}
}

Explanation / Answer

HI friend, I have fixed it.

Instead of "remove" method of Queue, you should use "pool" method.

import java.util.Stack;

import java.util.Scanner;

import java.util.Queue;

import java.util.LinkedList;

public class Palindrome {

   // This is a main method to test checkPalindrome method

   public static void main(String[] args) {

       String inputString;

       Scanner in = new Scanner(System.in);

       do {

           // please put your code to test checkPalindrome method here

           System.out.println("please enter any string ");

           inputString=in.nextLine();

           int res=checkPalindrome(inputString);

           if(res==0){

               System.out.println("string is a palindrome");

           }

           else{

               System.out.println("not a palindrome");

           }

           System.out.println("do you want to continue(y/n)");

           inputString=in.nextLine().toLowerCase();

       } while (inputString.equals("y") && inputString.length() == 1 );

       System.out.print("Bye!");

   }

   // This is checkPalindrome method. It checks if an input string is Palindrome or not.

   // It returns 0 if a string is a Palindrome. Otherwise, it returns a position of a character where it finds

   // a different value.

   // Pre-Condition: string must not be null.

   // Post-Condition: Return 0 if input string is a Palindrome. Return a positive number indicate the location where

   // a difference found.

   public static int checkPalindrome(String strValue) {

       Stack<Character> stack = new Stack<Character>();

       Queue<Character> queue = new LinkedList<Character>();

       int indexVal=-1;

       // check if string is null. If it is null, return a -1

       if(strValue!=null && strValue!=""){

           // normalize the string values to lower case, remove spaces

           strValue = strValue.toLowerCase().replaceAll("\W", "");

           for(int i=0;i<strValue.length();i++){

               stack.push(strValue.charAt(i));

               queue.add(strValue.charAt(i));

           }

           while(!stack.isEmpty()){

               char c=queue.poll();

               char d=stack.pop();

               if(d != c){ //checks if each and every character is equal

                   indexVal=strValue.indexOf(c);

                   break;

               }

               else{

                   indexVal=0;

               }

           }

       }

       else{

           indexVal=-1;;

       }

       // store data on stack/queue adts first

       // loop: comparing, retrieving text, terminate loop if stack is emptied or found a difference

       return indexVal;

   }

}


Please let me know in case of any issue.

Please rate my Answer!!

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote