扫描器循环中断 + 正则表达式问题

huangapple go评论74阅读模式
英文:

Scanner loop breaks + Regex issue

问题

try {
    Pattern pattern = Pattern.compile("[Y|y][E|e][S|s]");

    while (true) {
        String word = "";
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the word without spaces");

        if (sc.hasNext("(?>\\p{Alpha})+")) {
            word = sc.next(); 
            System.out.println(word);
            System.out.println("The word is correct");
            String text2;
            StringBuilder sb = new StringBuilder();
            text2 =  sb.append(word).reverse().toString();

            if (word.equalsIgnoreCase(text2)) {
                System.out.println("Yes, it's a palindrome." + " " + "Want to try another one?");
                System.out.println("Enter yes if you want to continue" + " or " + "enter any symbol if no");
                if (sc.hasNext(pattern)) {
                } else {
                    break;
                }
            } else {
                System.out.println("No, it's not a palindrome." + " " + "Want to try another one?");
                System.out.println("Enter yes if you want to continue" + " or " + "enter any symbol if no");
                if (sc.hasNext(pattern)) {
                } else {
                    break;
                }
            }
        } else {
            word = sc.next();
            System.out.println("It's not a word");
            System.out.println("Want to try another one?");
            System.out.println("Enter yes if you want to continue" + " or " + "enter any symbol if no");
            if (sc.hasNext(pattern)) {
            } else {
                break;
            }
        }
    }
} catch (NoSuchElementException ex) {
    System.out.println(ex.getMessage());
}
英文:

This task is whether a word is a palindrome, and I use java 11.

After entering of the word with spaces the code gives me an answer, but while loop breaks and the code "finishes with exit code 0" If I choose word = sc.next(). But If I write word = sc.nextLine() - It executes well and begins loop again.

I can't understand why, cause I can't debug this part of code, It's just skipped. And I don't understand why this happens. So can somebody explain to me what am I doing wrong?

Maybe there's a way to avoid this spaces?

P.S. I'm new to programming. Thanks to everyone.

    try {
Pattern pattern = Pattern.compile("[Y|y][E|e][S|s]");
while (true) {
String word = "";
Scanner sc = new Scanner(System.in);
System.out.println("Enter the word without spaces");
if (sc.hasNext("(?>\\p{Alpha})+")) {
word = sc.next(); 
System.out.println(word);
System.out.println("The word is correct");
String text2;
StringBuilder sb = new StringBuilder();
text2 =  sb.append(word).reverse().toString();
if (word.equalsIgnoreCase(text2)) {
System.out.println("Yes, it's a palindrome." + " " + "Want to try another one?");
System.out.println("Enter yes if you want to continue" + " or " + "enter any symbol if no");
if (sc.hasNext(pattern)) {
}
else {
break;
}
}
else {
System.out.println("No, it's not a palindrome." + " " + "Want to try another one?");
System.out.println("Enter yes if you want to continue" + " or " + "enter any symbol if no");
if (sc.hasNext(pattern)) {
}
else {
break;
}
}
}
else {
word = sc.next();
System.out.println("It's not a word");
System.out.println("Want to try another one?");
System.out.println("Enter yes if you want to continue" + " or " + "enter any symbol if no");
if (sc.hasNext(pattern)) {
}
else {
break;
}
}
}
}
catch (NoSuchElementException ex) {
System.out.println(ex.getMessage());
}
}

答案1

得分: 0

你的正则表达式 [Y|y][E|e][S|s] 可以进行改进吗?

改用 [Yy][Ee][Ss][] 指定了一个字符类,括号内的所有字符表示其中的任意一个,也就是默认情况下它们会隐式地被视为 OR 关系。点击这里了解更多信息。

如何判断输入是否包含空格?

" " 传递给函数 String#contains 来判断。

演示:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.print("输入一个没有空格的单词:");
            String word = sc.nextLine();
            if (word.contains(" ")) {
                System.out.println("这不是一个单词");
            } else {
                System.out.println("这是一个正确的单词");
                StringBuilder reverse = new StringBuilder(word).reverse();
                System.out.println(word.equalsIgnoreCase(reverse.toString()) ? "回文" : "不是回文");
            }
            System.out.println("想再试一次吗?");
            System.out.print("输入 yes 继续,或输入其他任何符号退出:");
            if (!sc.nextLine().matches("[Yy][Ee][Ss]")) {
                break;
            }
        }
    }
}

一个示例运行:

输入一个没有空格的单词:Hello World
这不是一个单词
想再试一次吗?
输入 yes 继续,或输入其他任何符号退出:yes
输入一个没有空格的单词:HELLO
这是一个正确的单词
不是回文
想再试一次吗?
输入 yes 继续,或输入其他任何符号退出:YES
输入一个没有空格的单词:Malayalam
这是一个正确的单词
回文
想再试一次吗?
输入 yes 继续,或输入其他任何符号退出:Yes
输入一个没有空格的单词:papa
这是一个正确的单词
不是回文
想再试一次吗?
输入 yes 继续,或输入其他任何符号退出:n
英文:

What can you improve in your regex, [Y|y][E|e][S|s]?

Use [Yy][Ee][Ss] instead. The [] specifies a character class and all the characters inside it mean one of them i.e. by default they come with an implicit OR. Check this to learn more about it.

How can you determine if the input contains space?

Pass " " to the function, String#contains to determine it.

Demo:

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (true) {
System.out.print("Enter a word without spaces: ");
String word = sc.nextLine();
if (word.contains(" ")) {
System.out.println("It's not a word");
} else {
System.out.println("The word is correct");
StringBuilder reverse = new StringBuilder(word).reverse();
System.out.println(word.equalsIgnoreCase(reverse.toString()) ? "Palindrome" : "Not a palindrome");
}
System.out.println("Want to try another one?");
System.out.print("Enter yes if you want to continue or any other symbol otherwise: ");
if (!sc.nextLine().matches("[Yy][Ee][Ss]")) {
break;
}
}
}
}

A sample run:

Enter a word without spaces: Hello World
It's not a word
Want to try another one?
Enter yes if you want to continue or any other symbol otherwise: yes
Enter a word without spaces: HELLO
The word is correct
Not a palindrome
Want to try another one?
Enter yes if you want to continue or any other symbol otherwise: YES
Enter a word without spaces: Malayalam
The word is correct
Palindrome
Want to try another one?
Enter yes if you want to continue or any other symbol otherwise: Yes
Enter a word without spaces: papa
The word is correct
Not a palindrome
Want to try another one?
Enter yes if you want to continue or any other symbol otherwise: n

huangapple
  • 本文由 发表于 2020年7月23日 22:12:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/63056320.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定