can help me in this code
i made it to check if a given number is plindrom or not

import java .util.Scanner;
public class PNumber {
	public static void main(String[] args) {
		System.out.println("Enter size ");
		Scanner input = new Scanner(System.in);

		int size  = input.nextInt();
	 int [] array=new int  [size];
	 		System.out.print("Please enter a number: ");
	 for ( int i=0;i<=size;i++)
	 	array[i]=input.nextInt();
for ( int i=0;i<=size;i++){
if(array[i]==array[i].length-1)
	System.out.print("plindrom ");
	else
			System.out.print("not plindrom ");

}}}

Recommended Answers

All 21 Replies

for ( int i=0;i<=size;i++){
if(array[i]==array[i].length-1)
    System.out.print("plindrom ");
else
    System.out.print("not plindrom ");

try to check the values of array[i] and array[i].length-1 at the loop to know what your program is doing

Please explain what your problem is?

check if a given number is plindrom or not

What is the code supposed to be doing with all the numbers it reads in?

I do not understand what you wanted to do with this:

for ( int i=0;i<=size;i++){
     if(array[i]==array[i].length-1)
        System.out.print("plindrom ");
     else
        System.out.print("not plindrom ");
 
}

You are just comparing the number stored in array with the length of the array minus one.So just to be clear a number is a palindrom if its equal with the mirror of it.
Examples:
3245 is not a palindrom because 3245 is not equal to 5423
121 is a palindrom because 121 is equal to 121
Here is how you reverse the number:

int x=3221,aux=0;
while(x!=0)
{
    aux=aux*10+x%10; 
    x/=10;
}
System.out.println(aux);

This will output the reverse of x(3221) witch is 1223.

I do not understand what you wanted to do with this:

for ( int i=0;i<=size;i++){
     if(array[i]==array[i].length-1)
        ...
}

You are just comparing the number stored in array with the length of the array minus one.

Not even that really. It's just some invalid uncompilable code that doesn't actually do anything. OP was obviously struggling towards something more like
if(array==array[length-1-i])

code should compare between original array ,, and reverse array to check
they are plindrome or not ...


error:
PNumber.java:13: int cannot be dereferenced
if(array==array.length-1)
^
1 error

i try to use equals but
i know iam going at wrong way

array.equals(array.length-1)

See my previous post
if(array==array[array.length-1-i])

it is give me this

;
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:840)
at java.util.Scanner.next(Scanner.java:1461)
at java.util.Scanner.nextInt(Scanner.java:2091)
at java.util.Scanner.nextInt(Scanner.java:2050)
at PNumber.main(PNumber.java:7)

Your scanner is looking for an int, but it found something else.

yah i found it because i was forget and i enter char instead of number
--
i run it . and enter size and number
but it is not give me plindrom or not
it show this
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at PNumber.main(PNumber.java:10)

for ( int i=0;i<=size;i++){

Java arrays start at element 0 and go up to element (size -1) - now check your loop termination condition i<=size

can i use method equals with array

I don't understand your question. Can you show the code you wan to use? Have you tried doing it and see if it works?

The equals method could be used to see if two array references are pointing to the same array. It would not look at the contents of the arrays.

int[] ar1 = {1};
      int[] ar3 = ar1;
      System.out.println(ar1.equals(ar3));// true >>> ar3 equals ar1

this is my code with equals method

import java .util.Scanner;
public class PNumber {
	public static void main(String[] args) {
		System.out.println("Enter size ");
		Scanner input = new Scanner(System.in);
		int size = input.nextInt();
	 int [] array=new int [size];
	 		System.out.print("Please enter a number: ");
	 for ( int i=0;i<=size;i++)
	 	array[i]=input.nextInt();
int []arreverse=array;
if (array.equals(arreverse))
	System.out.print("plindrom ");
	else
			System.out.print("not plindrom ");

}}

What do you expect the equals() method to do?
Because the statement on line 11 sets the two array references to point to the same array, the equals method on line 12 will always be true.

so its can be reverse array

int []arreverse=array.length;

Does that statement compile?

What do you expect the equals() method to do?
Why are you using it?

actuly it is give me incobatable type :(

so how i can write it

so how i can write it

Can you Explain what you are trying to do?

This is my opinion:
"programing"-who made this thread- want check if one number is palindrome or not!
And, by using equals() method we cant solve with large number that have 50 digits or more!
We may treat input as a string, and it's not difficult to determine that string is palindrome or not, right?
One error we may catch that is when we enter a non-digit character, to avoid this error, just check every character of the string!
---
Songokute

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.