Home replaceAll and loops?
Reply: 1

replaceAll and loops?

user9352512
1#
user9352512 Published in 2018-02-13 02:21:52Z

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.

aKilleR
2#
aKilleR Reply to 2018-02-13 03:01:01Z

Totally agreed with @Ilya Bursov comment,

You should use in.nextLine() instead of in.next() , there are big difference between both methods

next() can read the input only till the space. It can't read two words separated by a space. Also, next() places the cursor in the same line after reading the input.

nextLine() reads input including space between the words (that is, it reads till the end of line \n). Once the input is read, nextLine() positions the cursor in the next line

Try like this ,

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.nextLine()) {
      //Prompt for input
      System.out.println();
      System.out.print("Enter a word or phrase: ");
      String phrase = in.nextLine();

      //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): ");
    }
  }
}
You need to login account before you can post.

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

© 2016 Powered by mzan.com design MATCHINFO