-1

I am new here. I would be grateful for some help, I am trying to write a program to find the palindromes in a sentence, this is what i have so far. However, whenever I execute it, the palindrome count comes to zero and System.out.println in the for and ifs do not take place, I cannot understand what is wrong. we have to use for loop, and not while.

import java.util.Scanner;

class palcheck {
    public void main() {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the sentence please...");// input sentece
        String s = sc.next();// the sentence

        String wordback = ""; //reverse of a word of the sentence
        int count = 0; // palindrome count
        String word = "";// for a word of the sentence
        int a; // counter
        String s2 = null; //sentence but with removed full stop

        if (s.charAt(s.length() - 1) == '.')// checking if ends with fullstop

        {
            s2 = s.substring(0, (s.length() - 1));
        }// sentence without fullstop
        System.out.println(s2);
        String s3 = " " + s2 + " ";
        System.out.println(s3);// added a space;
        for (int c = 0; c < s3.length(); c++) {
            char isspace = s3.charAt(c);
            if (isspace == ' ')// checking
            {
                char x = ' ';// initilaizing
                for (a = s3.indexOf(isspace); x != ' '; a++) {
                    x = s3.charAt(a); //collecting letters for word
                    word = word + x; // forming word
                    System.out.println("word is " + word);// the formed word
                }
                int l2 = word.length();
                for (int i = l2 - 1; i >= 0; i--) {
                    wordback = wordback + word.charAt(i);// forming reverse word
                    System.out.println("reverse word is " + wordback);
                    if (word.equalsIgnoreCase(wordback)) {
                        count = count + 1;
                    }// increasing palindrome count
                    word = null;// resetting value
                    wordback = null;//resetting value
                }
            }
        }
        System.out.println("the no of palindromes is " + count);
    }
}

Edit: I have tried to change the names of the variables:

import java.util.Scanner;

class palcheck {
    public void main() {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the sentence please...");// input sentece
        String sentence = sc.next();// the sentence

        String wordreverse = ""; //reverse of a word of the sentence
        int count = 0; // palindrome count
        String word = "";// for a word of the sentence
        int a; // counter
        String sentencewithoutfullstop = null; //sentence but with removed full stop

        if (sentence.charAt(sentence.length() - 1) == '.')// checking if ends with fullstop

        {
            sentencewithoutfullstop = sentence.substring(0, (sentence.length() - 1));
        }// sentence without fullstop
        System.out.println(sentencewithoutfullstop);
        String sentencewithspace = " " + sentencewithoutfullstop + " ";
        System.out.println(sentencewithspace);// added a space;
        for (int c = 0; c < sentencewithspace.length(); c++) {
            char isspace = sentencewithspace.charAt(c);
            if (isspace == ' ')// checking
            {
                char letter = ' ';// initilaizing
                for (a = sentencewithspace.indexOf(isspace); letter != ' '; a++) {
                    letter = sentencewithspace.charAt(a); //collecting letters for word
                    word = word + letter; // forming word
                    System.out.println("word is " + word);// the formed word
                }
                int length2 = word.length();
                for (int i = length2 - 1; i >= 0; i--) {
                    wordreverse = wordreverse + word.charAt(i);// forming reverse word
                    System.out.println("reverse word is " + wordreverse);
                    if (word.equalsIgnoreCase(wordreverse)) {
                        count = count + 1;
                    }// increasing palindrome count
                    word = null;// resetting value
                    wordreverse = null;//resetting value
                }
            }
        }
        System.out.println("the no of palindromes is " + count);
    }
}

EDIT: I have redone the program, but its not getting executed. I am using bluej and whenever i am selected void(main), the java virtual machine keeps running and i have to finally terminate it.

Is there a problem in the program?

import java.util.Scanner;

class cal {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        String input = sc.next();
        String word = null;
        String reverse = null;
        int count = 0;
        if (input.endsWith(".")) {
            String Sent2 = input.substring(0, (input.length() - 1));
            String sent3 = " " + Sent2 + " ";
            int l = sent3.length();
            for (int i = 0; i < l; i++) {
                if (sent3.charAt(i) == ' ') {
                    while (sent3.charAt(i + 1) != ' ') {
                        char h = sent3.charAt(i + 1);
                        word = word + h;
                        int l2 = word.length();
                        for (int j = l2 - 1; j >= 0; j--) {
                            char hg = word.charAt(j);
                            reverse = reverse + hg;
                            if (word.equalsIgnoreCase(reverse)) {
                                System.out.println("palindrome :" + word);
                                count++;
                            }
                        }
                    }
                }
            }
        } else {
            String sent3 = " " + input + " ";
            int l = sent3.length();
            for (int i = 0; i < l; i++) {
                if (sent3.charAt(i) == ' ') {
                    while (sent3.charAt(i + 1) != ' ') {
                        char h = sent3.charAt(i + 1);
                        word = word + h;
                        int l2 = word.length();
                        for (int j = l2 - 1; j >= 0; j--) {
                            char hg = word.charAt(j);
                            reverse = reverse + hg;
                            if (word.equalsIgnoreCase(reverse)) {
                                System.out.println("palindrome :" + word);
                                count++;
                            }
                        }
                    }
                }
            }
        }
    }
}
DimaSan
  • 12,264
  • 11
  • 65
  • 75
  • 1
    Use a debugger, divide your code to understandable and short methods... Much more to do before posting your question here. – Idos Dec 04 '16 at 09:38
  • 2
    And hint: look into properly formatting your code. This is stuff is 10 times harder to read than it ought to be. And: use better names for your variables. s1, s2, s3 ... says **nothing** about the content of those things. Finally: you can use String.split(" ") in order to divide your sentence into parts; you dont have to manually scann for space characters! – GhostCat Dec 04 '16 at 09:49
  • @GhostCat thank you very much! I have tried to name the variables better and will upload it. However we have not done the String.split function in school yet, and i am not sure whether or not our teacher will approve of , however i shall try again. Thanks! – viktor nikiforov Dec 04 '16 at 09:57
  • That is ok. It was just a suggestion. The one thing I really recommend: if you are allowed to, create your own little helper functions. You should try to write many small methods; instead of putting **everything** into your main class. – GhostCat Dec 04 '16 at 10:00
  • @GhostCat Thank you again! However we have not done programs using methods yet, and i am not sure how they work.. I shall try asking my classmates for help. ^^ – viktor nikiforov Dec 04 '16 at 10:05
  • Viktor, stating that you have not don String.split in class is not the right approach. It is perfectly understandable, since you are learning approaches as well, but you should use whatever you can, including String.split to solve your problem. When it is solved, you might think about the teacher not approving it and implementing your own method instead. But at that point you already have something presentable. – Lajos Arpad Dec 04 '16 at 11:24

2 Answers2

0

please avoid Systax Error in code :

In Java , main function must have the following

public static void main(String args[])
{

and for loop u mentioned condition never satisfied .because before looping u initialized like ,

char letter = ' ' ;// initilaizing
 for( a = sentencewithspace.indexOf(isspace);  letter !=' ' ;a++)

once, loop condition satisfised then only inside block to be executed.

Please dont use indexOf() in looping . Reason is java throw StringIndexOutOfBoundsException when String index out of range .

Refer link : http://www.vinaysingh.info/palindromic-words/

how to count the number of words of the same(PALINDROME) in java

Community
  • 1
  • 1
maha lakshmi
  • 57
  • 1
  • 2
  • 9
0

This code assumes you have an input String and finds the palindromes inside it.

String word = "";
String inverse = "";
for (int index = 0; index < input.length(); index++) {
    if (input.charAt(index) == " ") {
        if ((word.length() > 0) && (word.equals(inverse))) {
            System.out.println("Palindrome: " + word);
        }
        word = "";
        inverse = "";
    } else {
        char current = input.charAt(index);
        word += current;
        inverse = current + inverse;
    }
}
if ((word.length() > 0) && (word.equals(inverse))) {
    System.out.println("Palindrome: " + word);
}
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
  • Thank you very much! However whenever I try to execute it in this way, only the first word gets recognized as a palindrome, if it is a palindrome. is there a way to fix this? – viktor nikiforov Dec 04 '16 at 14:27
  • @viktornikiforov, can you give me an example input where the bug you are talking about occurs and can you describe the output you get? – Lajos Arpad Dec 04 '16 at 18:43
  • Aah its ok, I understood where I was going wrong!! ^_^; I was taking sc.next() instead of sc.nextLine() and thus it was not reading the full sentence.. Spasibo! – viktor nikiforov Dec 05 '16 at 11:09
  • @viktornikiforov, you are welcome. If my answer solved your problem, then you might consider accepting it by clicking on the tick mark below the vote number on the left. – Lajos Arpad Dec 06 '16 at 08:18