Here is the completed program. The issue is that when I enter a null string, it
ID: 3741460 • Letter: H
Question
Here is the completed program. The issue is that when I enter a null string, it outputs "The string is a palindrome" when it should output "Input string is null". Can someone please help me fix this mistake? Thank you!
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 = new String("");
Scanner in = new Scanner(System.in);
do {
// please put your code to test checkPalindrome method here
System.out.print("Please enter a string of characters: ");
inputString = in.nextLine();
int index = checkPalindrome(inputString);
if(index == -1) {
System.out.println("Input string is null");
}else if(index > 0) {
System.out.println("The given string is not a palindrome, since the symbol at position " +index
+ " from the left is different from the symbol at position "+index+" from the right.");
}else{
System.out.println("The given string is a palindrome.");
}
System.out.print("Want to examine another string? (y/n): ");
inputString = in.nextLine();
} 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>();
// check if string is null. If it is null, return a -1
if(strValue == null)
return -1;
// normalize the string values to lower case, remove spaces
strValue = strValue.toLowerCase().replaceAll("\W", "");
// store data on stack/queue adts first
for(int i=0; i<strValue.length(); i++) {
char c = strValue.charAt(i);
stack.push(c);
queue.add(c);
}
// loop: comparing, retrieving text, terminate loop if stack is emptied or found a difference
int indexVal = 0;
while(!stack.isEmpty()) {
if(stack.peek() != queue.peek()) {
return (indexVal+1);
}
indexVal++;
stack.pop();
queue.poll();
}
return 0;
}
}
Explanation / Answer
Palindrome.java
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 = new String("");
Scanner in = new Scanner(System.in);
do {
// please put your code to test checkPalindrome method here
System.out.print("Please enter a string of characters: ");
inputString = in.nextLine();
int index = checkPalindrome(inputString);
if(index == -1) {
System.out.println("Input string is null");
}else if(index > 0) {
System.out.println("The given string is not a palindrome, since the symbol at position " +index
+ " from the left is different from the symbol at position "+index+" from the right.");
}else{
System.out.println("The given string is a palindrome.");
}
System.out.print("Want to examine another string? (y/n): ");
inputString = in.nextLine();
} 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>();
// check if string is null. If it is null, return a -1
if(strValue == null||strValue.trim().equals(""))
return -1;
// normalize the string values to lower case, remove spaces
strValue = strValue.toLowerCase().replaceAll("\W", "");
// store data on stack/queue adts first
for(int i=0; i<strValue.length(); i++) {
char c = strValue.charAt(i);
stack.push(c);
queue.add(c);
}
// loop: comparing, retrieving text, terminate loop if stack is emptied or found a difference
int indexVal = 0;
while(!stack.isEmpty()) {
if(stack.peek() != queue.peek()) {
return (indexVal+1);
}
indexVal++;
stack.pop();
queue.poll();
}
return 0;
}
}
Output:
Please enter a string of characters:
Input string is null
Want to examine another string? (y/n): y
Please enter a string of characters: madam
The given string is a palindrome.
Want to examine another string? (y/n): n
Bye!