Home replaceAll and loops?
Reply: 0

replaceAll and loops?

user7842
1#
user7842 Published in September 20, 2018, 10:22 pm

My Computer Science class assignment requires that I write a program which determines if a word or phrase is a palindrome (is the same forward and backwards, ie "noon"). As part of this, I have to write a method which removes all punctuation and spaces, so they are not counted in determining if it is a palindrome. It also runs on a loop, allowing the user to input as many phrases they want until they indicate they're done. My problem is that when the word/phrase entered contains a space, somehow it terminates the loop and doesn't allow more input. The program works just fine, as long as the input has no spaces. Here's my code:

In class RecursivePalindrome:

public String removePunctuation(String s){
    s = s.replaceAll("\\.","");
    s = s.replaceAll("!","");
    s = s.replaceAll(",","");
    s = s.replaceAll(" ","");
    s = s.replaceAll("'","");
    s = s.replaceAll("-","");
    s = s.replaceAll("\\?","");
    return s;
}

public boolean isPalindrome(String s) {

    s = removePunctuation(s);

    String firstChar = s.substring(0,1);
    String lastChar = s.substring(s.length()-1);

    if (s.length() == 1){
        return true;
    }

    if (s.length() == 2 && firstChar.equalsIgnoreCase(lastChar)){
        return true;
    }

    if (!firstChar.equalsIgnoreCase(lastChar)){
        return false;
    }

    return isPalindrome(s.substring(1, s.length() - 1));
}

In class RecursivePalindromeTester:

public static void main(String[]args){

    //Create objects
    Scanner in = new Scanner(System.in);
    RecursivePalindrome palindrome = new RecursivePalindrome();

    //Output
    for (String again = "Y"; again.equalsIgnoreCase("Y"); again = in.next())
    {
        //Prompt for input
        System.out.println();
        System.out.print("Enter a word or phrase: ");
        String phrase = in.next();

        //Output
        if (palindrome.isPalindrome(phrase)){
            System.out.println("This is a palindrome.");
        }
        else
            System.out.println("This is not a palindrome.");

        System.out.print("Another word or phrase? (Y/N): ");
    }
}

The output should be:

"Enter word or phrase: <input>mom- mom!
This is a palindrome
Another word or phrase? (Y/N): <input>Y
Enter a word or phrase: <input>Dog?
This is not a palindrome
Another word or phrase? (Y/N): <input>N"
Terminate

But instead I get:

"Enter word or phrase: <input>mom- mom!
This is a palindrome
Another word or phrase? (Y/N):"
Terminate

I really have no idea why a space would cause the loop to terminate, especially since it doesn't do this with any other punctuation.

share|improve this question
  • 2
    replace in.next() with in.nextLine() – Iłya Bursov Feb 13 at 2:44

1 Answer 1

active oldest votes
up vote 0 down vote accepted
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.304803 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO