0

I am trying to write a program that checks if a string is a palindrome and so far I know I am on the right path but when I enter my code it keeps on running for ever. I don't know what the problem is and would like help finding out the solution. In my program I want the user to enter word or words in the method Printpalindrome and then the program should know if the string is a palindrome or not.

Here is my code:

      ...
      Scanner console = new Scanner (System.in);
      String str = console.next(); 
      Printpalindrome(console, str);
    }

    public static void Printpalindrome(Scanner console, String str) {
      Scanner in = new Scanner(System.in);       
      String original, reverse = "";



      str = in.nextLine();

      int length = str.length();

      for ( int i = length - 1; i >= 0; i-- ) {
        reverse = reverse + str.charAt(i);
      }

      if (str.equals(reverse))
        System.out.println("Entered string is a palindrome.");  

      }
   }
Benjy Kessler
  • 7,356
  • 6
  • 41
  • 69
Cooldude
  • 63
  • 1
  • 1
  • 7
  • 1
    Uhm, this question has been asked tens and tens of times already and answers exist; have you searched? – fge Apr 20 '15 at 15:38
  • 1
    Yes and no, I know this questions has been asked before but I want to learn from my mistakes and want to understand what I am doing wrong. – Cooldude Apr 20 '15 at 15:39
  • 2
    You seem to be discarding the original value of `n` inside `Printpalindrome` without using it - why? – JonK Apr 20 '15 at 15:41
  • How am I discarding the original value of n. – Cooldude Apr 20 '15 at 15:43
  • You're overwriting the parameter when you call `n = in.nextLine();`. Thus whatever you passed in won't be what gets checked. – JonK Apr 20 '15 at 15:45
  • I don't see any needs on in.nextLine() in PrintPalindrome method. You also need to follow conventsions while naming your method. – Jimmy Apr 20 '15 at 15:45
  • 2
    possible duplicate of [Check string for palindrome](http://stackoverflow.com/questions/4138827/check-string-for-palindrome) – nana Apr 20 '15 at 15:45
  • 2
    People should not propose better implementations, that's not the question (OP rightfully wants to know why his program doesn't work) and is totally off topic. – Denys Séguret Apr 20 '15 at 15:48

5 Answers5

3

Because of this line:

n = in.nextLine();

your program is waiting for a second input, but you already got one before entering the function.

Remove this line and it works.

Here's your program, cleaned (and tested) :

public static void main(String[] args){
    Scanner console = new Scanner (System.in);
    String n = console.next(); 
    Printpalindrome(n);
}

public static void Printpalindrome(String n){
    String reverse = "";
    for ( int i = n.length() - 1; i >= 0; i-- ) {
        reverse = reverse + n.charAt(i);
        System.out.println("re:"+reverse);
    }
    if (n.equals(reverse))
        System.out.println("Entered string is a palindrome."); 
    else
        System.out.println("Entered string is NOT a palindrome."); 
}

Of course, this isn't the best algorithm, but you already know there are many QA on SO with faster solutions (hint: don't build a string, just compare chars).

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
0

This can be implemented in a far more efficient manner:

boolean isPalindrom(String s){
    if (s == null /* || s.length == 0 ?*/) {
        return false;
    }

    int i = 0, j = s.length() - 1;
    while(i < j) {
        if(s.charAt(i++) != s.charAt(j--)) {
              return false;
        }
    }
    return true;
}

The argument for PrintPalindrom is ignored. You read another value with `in.nextLine()'. Which is the reason for your issues.

UnknownJoe
  • 599
  • 5
  • 14
  • 30
  • Thats just the same code, with the only difference that mine needs less calculations. Mine breaks off aswell, as soon as the middle of the string is reached `(i < j)`. –  Apr 20 '15 at 15:47
  • Bruh, are you trolling, or don't you understand my code? –  Apr 20 '15 at 15:50
0

Remove

Scanner in = new Scanner(System.in);

and

n = in.nextLine();

from Printpalindrome function

and it should work.

justcurious
  • 254
  • 3
  • 15
  • public static void Printpalindrome(Scanner in, String n){ // Scanner in = new Scanner(System.in); String orginal, reverse = ""; //n = in.nextLine(); int length = n.length(); for ( int i = length - 1; i >= 0; i--) reverse = reverse + n.charAt(i); if (n.equals(reverse)) System.out.println("Entered string is a palindrome."); else System.out.println("Not"); } public static void main(String args[]) { Scanner console = new Scanner (System.in); String n = console.next(); Printpalindrome(console,n ); } – justcurious Apr 20 '15 at 15:58
0

I tried your code and what i observed was that : first of all you are making a string to enter on the line 2 of your code:

String n=console.next();

next the the program again goes to waiting when this line gets executed:

n = in.nextLine();

actually this particular line is also expecting an input so that is why the program halt at this point of time. If you enter your String to be checked for palindrome at this point of time you would get the desired result . But I would rather prefer you to delete the line

n = in.nextLine();

because, with this, you would have to enter two words which are ambiguous.

Hariprasad
  • 3,556
  • 2
  • 24
  • 40
0

Ur code with some correction:-

import java.util.*;

class Palindrome
{
 public static void main(String args[])
 {
  String original, reverse = "";
  Scanner in = new Scanner(System.in);

  System.out.println("Enter a string to check if it is a palindrome");
  original = in.nextLine();

  int length = original.length();

  for ( int i = length - 1; i >= 0; i-- )
     reverse = reverse + original.charAt(i);

  if (original.equals(reverse))
     System.out.println("Entered string is a palindrome.");
  else
     System.out.println("Entered string is not a palindrome.");

 }
}