Make a static method in the main class that writes on the screen all even numbers from 1 to 10000 that are palindromes

1

This is my method:

public static void B()
{
    int vet[]=new int [100];
    int vet2[]=new int [vet.length/2];
    for(int i=0;i<=vet.length;i++)

    {
        vet[i]=i+1;
    }
    for(int i=0;i<vet.length;i++)
    {
        int rest = vet[i]%2;
        if(rest==0)
        {
            vet2[i]=vet[i];
        }
    }
    for (int i = 0; i <= vet2.length; i++) 
    {
        if(vet2[i]==(vet2[i-1])-i)
        {
            System.out.println(vet2[i]+"É PALINDROMO");
        }
    }

And this is the error that appears:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 100
  at lista1.pkg05.Lista105.B(Lista105.java:59)
  at lista1.pkg05.Lista105.main(Lista105.java:15)
C:\Users\Pichau\AppData\Local\NetBeans\Cache.2\executor-snippets\run.xml:53:
     

Java returned: 1

    
asked by anonymous 09.03.2017 / 11:04

3 answers

4

The algorithm has an error in for because it reads an extra number. You can not buy with <= to size because the count starts at 0, it has to be < . It would also work to use this comparison by subtracting 1, but this generates a greater computational effort and it does not make sense to use it like this.

Access to check for palindromes has a wrong formula and causes a problem as well. I've improved, but I think it's still not what you want, because it's to check if the array is palindrome and not say about each element, but will know.

One of the arrays has no function in the algorithm other than to complicate.

For this algorithm it will never be palindrome, but I hope it will generate the array with some other algorithm. Would it be to check if the digits of each number are palindrome? There the algorithm is different. But it already falls into another problem.

I just kept the first 100 as done in the question, maybe to make it easier to test, to go up to 10000, just change the size of the array .

I've put one organized in the code as well.

class HelloWorld {
    public static void main (String[] args) {
        int vet[] = new int[50];
        for (int i = 0; i < vet.length; i++) {
            if (i % 2 == 0) {
                vet[i] = (i + 1) * 2;
            }
        }
        for (int i = 0; i < vet.length; i++) {
        if (vet[i] == vet[vet.length - i - 1]) {
                System.out.println(vet[i] + " É PALINDROMO");
            }
        }
    }
}

See running on ideone . And at Coding Ground . Also put it on GitHub for future reference .

    
09.03.2017 / 12:52
1

The problem is in these two lines;

for(int i=0;i<=vet.length;i++)

for (int i = 0; i <= vet2.length; i++) 

The length property returns the total length of elements in the array, so when using to iterate an array as the above code, in an array of 10 positions it goes through 11 times from 0 to 10 thus giving the error ArrayIndexOutOfBoundsException in the eleventh iteration, to solve the problem follows the code below.

for(int i=0;i<=vet.length -1;i++)

for (int i = 0; i <= vet2.length -1; i++) 

This way the code will go 10 times from 0 to 9.

    
09.03.2017 / 11:15
1

Well, I'll address this in a more readable way.

public class HelloWorld {
   public static void main(String[] args) {
      Palindromas();
   }
   public static void Palindromas()
   {

       StringBuilder sb = new StringBuilder();//Alocar Espaço para um SB

       for(int i = 1; i <= 10000; i++) {

         sb.append(i);//Adicionar o número no sb

         sb.reverse();//Inverter o número

         if(
            i % 2 == 0 // se  é par
            && // E
            Integer.parseInt(sb.toString()) == i //Ele ao contrário é igual a ele normal
            ){
             System.out.println(i);//Printa
         }

         sb.setLength(0);//Limpa o Buffer pra reutiliza-lo no proximo item do loop

      };
   }
}

I just did it because I already had more answers, mine is a workaround;)

    
09.03.2017 / 14:11