Hello I need help with my program I cant seem to get the exp

Hello, I need help with my program. I can\'t seem to get the expected output. Here are my results for the program.

Program Output

Expected Output

Here is my program, which consists of two files.

1st file (This one does not need to be modified):

import java.io.*;

public class Assignment11

{

public static void main (String[] args) throws IOException

   {

   char input1;

   String line = new String();

   printMenu();

   InputStreamReader isr = new InputStreamReader(System.in);

   BufferedReader stdin = new BufferedReader(isr);

   do // will ask for user input

{

   System.out.println(\"What action would you like to perform?\");

   line = stdin.readLine();

   input1 = line.charAt(0);

   input1 = Character.toUpperCase(input1);

   if (line.length() == 1)

{

   // matches one of the case statements

   switch (input1)

{

   case \'E\': //Enter Problem parameters

   System.out.print(\"Please enter a string to check:\ \");

   String inputString = stdin.readLine().trim();

  

   //create an object of PalindromeChecker using the input string

   PalindromeChecker checker = new PalindromeChecker(inputString);

   checker.check();

   break;

   case \'Q\': //Quit

   break;

   case \'?\': //Display Menu

   printMenu();

   break;

   default:

   System.out.print(\"Unknown action\ \");

   break;

}

}

   else

{

   System.out.print(\"Unknown action\ \");

}

} while (input1 != \'Q\' || line.length() != 1);

   }

/** The method printMenu displays the menu to a user**/

public static void printMenu()

   {

   System.out.print(\"Choice\\t\\tAction\ \" +

\"------\\t\\t------\ \" +

\"E\\t\\tEnter String\ \" +

\"Q\\t\\tQuit\ \" +

\"?\\t\\tDisplay Help\ \ \");

}

}

2nd File (I marked what section I need help with ):

import java.util.Stack;

import java.util.LinkedList;

public class PalindromeChecker

{

private Stack charStack; //stack can contain characters

private LinkedList charQueue; //queue can contain characters

private String inputString;

//Constructor to initialize member variables

public PalindromeChecker(String inputString)

{

charStack = new Stack();

charQueue = new LinkedList();

this.inputString = inputString;

}

//The check method checks if an input string is a palindrome or not,

//and prints its result.

public void check()

   {

   boolean success = true; //success indicates whether palindrome or not

   boolean done = false; //it is used to stop the while loop

   int i = 0; //i wil be used as an index of the inputString

/***This section below is the code I need help with***/

   while (!done)

{

   //until the end of inputString or it encounters \'#\' character

   //take each character in inputString from left and add it to charStack and charQueue

       while(true){

   if(i == inputString.length())

   break;

   else if(inputString.charAt(i) == \'#\'){

   break;

   }

   char ch = inputString.charAt(i);

   charStack.push(ch);

   charQueue.add(ch);

   ++i;

   }

  

   //prints out the substring extracted from the input string using queue\'s toString method

   System.out.print(\"The input \" + charQueue.toString());

  

//until the charQueue or charStack becomes empty

//remove a character from each of charStack and charQueue, and check if they are same.

//If they are different, then print out the approproate message (\" is not a palindrome\ \")

//and also print which first set of two characters are different

//(\"The characters \' \' and \' \' do not match\ \ \") -- note that you need to print

//two such characters inbetween \' \'

//Also set success to false, so that the following \"if\" statement will be skipped

   char ch1, ch2;

   success = true;

   while(!charQueue.isEmpty()){

   ch1 = charQueue.pop();

   ch2 = charStack.pop();

   if(ch1 != ch2){

       System.out.print(\" is not a palindrome \ The characters \'\"

              + ch1 + \"\' and \'\" + ch2 + \"\' do not match\ \ \");

   success = false;

   break;

   }

   }

/***End section of code I need help with***/

if (success == true)

   System.out.print(\" is a palindrome\ \ \");

//i reaches the end of string, it is done processing the inputString

if (i == inputString.length())

done = true;

else

{

i++;

success = true; //set it back to true for the next substring to check

}

} //end of while loop

} //end of check( ) method

} // end of PalindromeChecker class

Solution

Hi,

The problem which is there in the code is if your string contains more than one palindrome string separated by \'#\', for example consider the string \"deified#racecar\". This has two palindrom strings in it deified and racecar.

For these type of cases the code above will not give the desired output.

Solution: The solution for this will be to clear the stack and queue before testing the second (and third) words.Otherwise letters from first word will invalidate your result.

I have modified the code snippet below which will guide you how to do it:

/*** This section below is the code i need help with ***/

while (!done) {

charStack.clear(); // Clear the stack. charQueue.clear(); //Clear the queue //until the end of input string or it encounters a \'#\' charactertake each char in input String from left and add it to //charStack and charQueue. while (true) {

if(i == inputString.length())

   break;

   else if(inputString.charAt(i) == \'#\'){

   break;

   }

   char ch = inputString.charAt(i);

   charStack.push(ch);

   charQueue.add(ch);

   ++i;

   }

//prints out the substring extracted from the input string using queue\'s toString method

   System.out.print(\"The input \" + charQueue.toString());

  

//until the charQueue or charStack becomes empty

//remove a character from each of charStack and charQueue, and check if they are same.

//If they are different, then print out the approproate message (\" is not a palindrome\ \")

//and also print which first set of two characters are different

//(\"The characters \' \' and \' \' do not match\ \ \") -- note that you need to print

//two such characters inbetween \' \'

//Also set success to false, so that the following \"if\" statement will be skipped

   char ch1, ch2;

   success = true;

   while(!charQueue.isEmpty()){

   ch1 = charQueue.pop();

   ch2 = charStack.pop();

   if(ch1 != ch2){

       System.out.print(\" is not a palindrome \ The characters \'\"

              + ch1 + \"\' and \'\" + ch2 + \"\' do not match\ \ \");

   success = false;

   break;

   }

   }

/***End section of code I need help with***/

Note: The changes done in this code as part of my answer is clearing the charStack and charQueue

Please let me know in case you face any difficulties in this answer.

Thanks & Regards,

Tushar

Hello, I need help with my program. I can\'t seem to get the expected output. Here are my results for the program. Program Output Expected Output Here is my pro
Hello, I need help with my program. I can\'t seem to get the expected output. Here are my results for the program. Program Output Expected Output Here is my pro
Hello, I need help with my program. I can\'t seem to get the expected output. Here are my results for the program. Program Output Expected Output Here is my pro
Hello, I need help with my program. I can\'t seem to get the expected output. Here are my results for the program. Program Output Expected Output Here is my pro
Hello, I need help with my program. I can\'t seem to get the expected output. Here are my results for the program. Program Output Expected Output Here is my pro
Hello, I need help with my program. I can\'t seem to get the expected output. Here are my results for the program. Program Output Expected Output Here is my pro

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site