Tejvir Singh
Tejvir Singh

Reputation: 101

Trying to solve a palindrome using integer arrays

I am writing a program that would help me find whether the number entered is a palindrome or not but i am trying it using arrays. And i would like to know if that is even possible?? And if it is possible then what am i doing wrong.

I have marked the code where i think the problem lies but feel free to suggest anything.!!!!

Thanks!!!

import java.util.Scanner;


public class palindrome
{
 public static void main(String args[])
 {
    int size = 10,i,j,flag=0;
    int num[] = new int[size];

    Scanner sc = new Scanner(System.in);
        System.out.println("Enter the size of the number ");
            size = sc.nextInt();
            System.out.println("Enter the number ");
    for(i=0;i<size;i++)
    {
        num[i]=sc.nextInt();
    }
    i=size-1;
    for(j=0;j<(size/2);j++,i--)
    {
        if(i>(size/2))
        {
                        if(num[i]==num[j])   
            {
                flag = 1;
            }
        }
    }
    if(flag==1)
    {
        System.out.println("The number is a palindrome");

    }
    else
        System.out.println("The number is not a palindrome ");
 }
} 

Edit: Guys the problem is actually solved because i was doing a blunder mistake i.e. i was asking the user to enter the number in the form of an arry but i was not actually entering the digits in the number one by one instead i was entering the whole number in the first iteration.

But still a lot of thanks for the replies. I would still try your ideas and let you guys know. Thanks

:)

Upvotes: 2

Views: 3775

Answers (4)

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186668

Yes it's possible, moreover, it's possible by using ArrayList, String - whatever you like. In order to write down a correct implementation, first decompose your current solution:

  // Extract a method, do not cram all code into main()
  // note: all you need is array, to get size of the array, put value.length
  private static boolean isPalindrome(int[] value) {
    ...
  }

  public static void main(String args[]) {
    int userInput[];
    ...

    if (isPalindrome(userInput)) {
      System.out.println("The number is a palindrome");
    }
    else {
      System.out.println("The number is not a palindrome");
    }
  } 

Now, let's implement isPalindrome():

  private static boolean isPalindrome(int[] value) {
    if (null == value)
      return true; //TODO: or false, or throw exception

    for (int i = 0; i < value.length / 2; ++i)
      if (value[i] != value[value.length - 1 - i])
        return false;

    return true;
  }

Upvotes: 2

gkrls
gkrls

Reputation: 2664

The easiest and most intuitive way (imo) to check for palindromes is through recursion. The idea is simple:

Is the first and last char the same?
YES Remove first and last char and check first and last char of the new String
NO There is no palindrome.

When the input is only 1 char then it's trivial.

Have a look at this code:

private void isPalindrome(String number){
    if(number.length() == 1){
        System.out.println("yes");
    }else if(number.charAt(0) == number.charAt(number.length()-1)){
        isPalindrome(number.substring(1, number.length()-1));
    }else{
        System.out.println("no");
    }
}

Testing with:

isPalindrome(String.valueOf(232)) Returns "yes"
isPalindrome(String.valueOf(23)) Return "no"

Of course this also works with Arrays just as easily. Replace the parameter with an array and search through the indices the same way. When cutting down the array just create a new smaller array without first and last index of the previous array.

Upvotes: 1

Olivier Gr&#233;goire
Olivier Gr&#233;goire

Reputation: 35417

Your class has several issues:

  • First you're not checking if a number is a palindrome or not. Your algorithm is flawed
  • Second, you're asking to enter a size but in the end, the user inputs it but you don't use it yourself. Instead, you're using that introduced value in the number array.

Here's how you should do it.

public class Palindrome {

  private static boolean isPalindrome(int[] array) {
    for (int i = 0, j = array.length-1; i < j; i++, j--) {
      if (array[i] != array[j]) {
        return false;
      }
    }
    return true;
  }

  public static void main(String args[]) {
    Scanner scanner = new Scanner(System.in);
    System.out.print("How many numbers do you want to enter? ");
    int size = scanner.nextInt();
    int[] numbers = new int[size];
    for (int i = 0; i < size; i++) {
      System.out.printf("Enter number %s: ", i+1);
      numbers[i] = scanner.nextInt();
    }
    if (isPalindrome(numbers)) {
      System.out.println("The number is a palindrome");
    } else {
      System.out.println("The number is not a palindrome");
    }
  }
}

Upvotes: 0

RDM
RDM

Reputation: 5066

Try

public boolean isPalindrome(int[] num){
    for(int i = 0 ; i < num.length/2 ; i++) {
        if(num[i]!=num[num.length-(i+1)]) return false;
    }
    return true;
}

Upvotes: 2

Related Questions