gundamjans
gundamjans

Reputation: 27

How to check whether a string is palindrome or not?

Im trying to code a palindrome. It has a user input string and the program will tell if it is a palindrome or not. I have searched the net for the code but it seems that when I have created another method, and when compiled the compiler says that "Nullpointerexception". Im trying this since yesterday, but it seems that I can't really understand it. I need it for our ICT class. THank you. this is my code.

import java.util.Scanner;

public class Palindrome {
   String word, reverse="";
   public static void main(String args[]){
       String word;
       Scanner in = new Scanner(System.in);
       System.out.println("Enter a string to check if it is a palindrome");
       word = in.nextLine();
       Palindrome check = new Palindrome();
       check.palindromeChecker();
   }

   public String palindromeChecker(){
      int length = word.length();

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

      if (word.equals(reverse))
         return "Palindrome";
      else
         return "Not a Palindrome";
   }
}

Upvotes: 1

Views: 8818

Answers (8)

Azer Shukurzade
Azer Shukurzade

Reputation: 1

package ispolidrom;

import java.util.Scanner;

public class IsPolidrom {


  public static void main(String[] args) {
     int number;
     Scanner input= new Scanner(System.in);
     System.out.print("\n Enter number: "); 
     number=input.nextInt();

           if(isPolidrome(number)) {
               System.out.printf("\n %d is polidrome",number);
           }

           else {
               System.out.printf("\n %d is NOT polidrome",number);

           }

}

public static boolean isPolidrome(int myNum)  {
    String numStr;
       numStr=Integer.toString(myNum);

    String revNum;
    StringBuffer buffer= new StringBuffer(numStr);
       revNum=buffer.reverse().toString();

    if(numStr.equals(revNum)) {
        return true;
    }

    else {
        return false;
    }
}
}

Upvotes: 0

trrrrr
trrrrr

Reputation: 1

 public class Palindrome
    {
        static String word, reverse="";
        public static void main(String args[])
        {
            Scanner in = new Scanner(System.in);
            System.out.println("Enter a string to check if it is a palindrome");
            word = in.nextLine();
            Palindrome check = new Palindrome();
            System.out.println(check.palindromeChecker());
        }

    public String palindromeChecker()
    {
        String reverse="";
        int length = word.length();

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

        if (word.equals(reverse))
            return "Palindrome";
        else
            return "Not a Palindrome";

    }
 }

Upvotes: 0

Dhunju_likes_to_Learn
Dhunju_likes_to_Learn

Reputation: 1366

Besides fixing the variable scope problem. I think the algorithm could be more efficient with the following code,

public class Palindrome {

public static void main(String[] args){

    Scanner keyboard = new Scanner(System.in);
    System.out.println("Enter a string to check if it is a palindrome");
    String word = keyboard.nextLine();

    if(isPalindrome(word))
        System.out.println(word + " is a Palindrome");
    else
        System.out.println(word + " is not a Palindrome" );


}


public static boolean isPalindrome(String word){
    int length = word.length();
    //loop until i reaches the midpoint index
    for(int i = 0; i < length/2 ; i++){
        //compare first char with last, second with second last
        //and so on until it reaches the mid point
        if(word.charAt(i) != word.charAt(length-i-1))
            return false;
    }
    return true;
}
}

Upvotes: 0

parag.rane
parag.rane

Reputation: 137

You are trying to access non-static String object inside the palindrome checker. Thats why it show NULLPointerException.

You should try the following way:

    public class Palindrome
    {
        static String word, reverse="";
        public static void main(String args[])
        {
            Scanner in = new Scanner(System.in);
            System.out.println("Enter a string to check if it is a palindrome");
            word = in.nextLine();
            Palindrome check = new Palindrome();
            System.out.println(check.palindromeChecker());
        }

        public String palindromeChecker()
        {
            String reverse="";
            int length = word.length();

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

            if (word.equals(reverse))
                return "Palindrome";
            else
                return "Not a Palindrome";

        }
     }

Upvotes: 0

sam
sam

Reputation: 1

{    Scanner s=new Scanner(System.in);    
System.out.println("enter the string:");    
String str=s.nextLine().toString();    
   String newStr=new StringBuilder(str).reverse().toString();    
   if(str.equals(newStr)){    
       System.out.println("is palindrome");    
   } else{    
       System.out.println("not a palindrome");    
   }    }    

}

Upvotes: 0

Matthias
Matthias

Reputation: 3592

The variable "word" in your static main method hides the instance variable "word" of your Palindrome class. So you are assigning the value from Scanner to the wrong variable.

To fix this error, you have several options:

The easy way out would be to remove the variable in your main method and redeclare the instance variable of your class Palindrome to be static. That however is not very elegant (and not object oriented). Better would be to pass the value you read as an argument to the method palindromeChecker.

There is of course a lot of other ways of doing this, but I am sure you will learn more ways once you get a deeper understanding of this language and programming in general.

Upvotes: 4

Ruchira Gayan Ranaweera
Ruchira Gayan Ranaweera

Reputation: 35557

This is enough to check palindrome

 String str="abccba";
   String newStr=new StringBuilder(str).reverse().toString();
   if(str.equals(newStr)){
       System.out.println("is palindrome");
   } else{
       System.out.println("not a palindrome");
   }

But still you want to do this in hard way change your code as follows

 public static void main(String[] args) throws ParseException {
    String word;
    Scanner in = new Scanner(System.in);
    System.out.println("Enter a string to check if it is a palindrome");
    word = in.nextLine();
    System.out.println(palindromeChecker(word));
}
public static String palindromeChecker(String word){
    int length = word.length();
    String reverse="";
    for ( int i = length - 1 ; i >= 0 ; i-- )
        reverse = reverse + word.charAt(i);
    if (word.equals(reverse))
        return "Palindrome";
    else
        return "Not a Palindrome";
}

Upvotes: 1

racumin
racumin

Reputation: 11

You can delete the local variable 'word' in the main method and change the global variable 'word' and 'reverse' to static. But it's better to pass a String parameter in your palindromeChecker method

Upvotes: 1

Related Questions