1

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

}
Nkimbiku
  • 13
  • 3
  • 1
    Hi! You've put way too much of your code here. You should narrow down the specific issue you have. Just looking at your first sentences, sc.nextLine(), will look at the line breaking. So that could part of it. Also in regards to your reg ex solution. I do not think that is the easiest way of solving it.Why are you doing it with reg ex? – GamingFelix Jul 23 '20 at 14:17
  • And why are you doing -> (sc.hasNext(pattern)) instead of just sc.next? I don't understand that either. – GamingFelix Jul 23 '20 at 14:22
  • I need "pattern" for result of decision whether person wants to try new words. And what about regex - I can create new "if" to test whether the word with spaces and decide, what to do with it, continue or not. Maybe it will help. – Nkimbiku Jul 23 '20 at 15:08
  • @GamingFelix is right. You do not need `sc.hasNext(pattern)` and this much complex code. If you are serious about programming, you should make all efforts to keep your code simple. – Arvind Kumar Avinash Jul 23 '20 at 15:19

1 Answers1

0

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
Arvind Kumar Avinash
  • 50,121
  • 5
  • 26
  • 72
  • No, I don't want just output, I want a loop (when I ask person If he/she wants to continue). And after unexpected "word with spaces" the code gives me solution, goes to part with asking me If I want to continue and the loop breaks. So I'm trying to understand why. I mentioned above when it happens. – Nkimbiku Jul 23 '20 at 14:45
  • @Nkimbiku - I've updated the answer based on your clarification. I hope it helps. – Arvind Kumar Avinash Jul 23 '20 at 15:11
  • It works, but your code has the same issue, when I change part "String word = sc.nextLine();" on "String word = sc.next();" . So, for example, I want to test a word/char before space and the loop breaks. – Nkimbiku Jul 23 '20 at 15:22
  • @Nkimbiku - You should never use `Scanner#next` in a loop; use `Sacnner#nextLine` instead. Check [this](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) to learn more about it. If you still want to use `Scanner#next`, you need to use an extra `Scanner#next` just to consume the new line character and it makes your code confusing. Feel free to comment in case of any further doubt/issue. – Arvind Kumar Avinash Jul 23 '20 at 15:24
  • Thank you :) I think, I understood it. – Nkimbiku Jul 23 '20 at 17:26