replaceAll和loops?

时间:2018-02-13 02:21:52

标签: java loops replaceall

我的计算机科学课程作业要求我编写一个程序,确定单词或短语是否是回文(向前和向后相同,即“正午”)。作为其中的一部分,我必须编写一个方法来删除所有标点符号和空格,因此它们不计入确定它是否是回文。它还在一个循环上运行,允许用户输入他们想要的短语,直到他们表明他们已经完成。我的问题是,当输入的单词/短语包含空格时,它会以某种方式终止循环并且不允许更多输入。只要输入没有空格,程序就可以正常工作。这是我的代码:

在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));
}

在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): ");
    }
}

输出应为:

"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

但我得到了:

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

我真的不知道为什么空格会导致循环终止,特别是因为它不会与任何其他标点符号一起执行此操作。

1 个答案:

答案 0 :(得分:0)

完全同意@Ilya Bursov的评论,

您应该使用in.nextLine()代替in.next(),这两种方法之间存在很大差异

next()只能读取输入直到空格。它不能读取由空格分隔的两个单词。此外,next()在读取输入后将光标放在同一行。

nextLine()读取输入,包括单词之间的空格(即,它读取直到行的结尾\ n)。读取输入后,nextLine()将光标定位在下一行

试试这个,

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): ");
    }
  }
}