anybody out there please help me out fix these bug in my code. i really couldnt figure out what's wrong with my code.

input should be in a string, and the output should tell wether its a palindrome or not

please!

import java.lang.*;
import java.util.*;
import java.io.*;

public class istak
{
    public static void main(String[] args)throws IOException
    {

	InputStreamReader wee= new InputStreamReader(System.in);
	BufferedReader stndin= new BufferedReader(wee);	

	    String word;
	    Stack s= new Stack();
	    int i,count=0;	
	
	    System.out.print("\nInput string: ");
	    word=stndin.readLine();
	    char array[]=word.toCharArray();	     

	

	for(i=0;i<array.length;i++)
	{
	 char val=array[i];
	   s.push(val);
	}

	for(i=0;i<array.length/2;i++)
	{
	char nxt=array[i];
	char temp=s.pop();
		if(nxt==temp)
		{
		 count++;
		}	   
	}


	if(count==array.length/2)
	{
	    System.out.println("PALINDROME");
        }

        else
        {
           System.out.println("NOT PALINDROME");
        }
	
   }

}

Recommended Answers

All 5 Replies

First of all you need to tell us what of mistakes your are having.
Is it compiler or it gives you wrong results and what kind.

We might not be in front a computer with java installed.

As for your error:

The pop method doesn't return a char value but an Object.

Stack.pop() returns an Object. And you are trying to put that value into a char. Since you know that that value is actually a char, you need to convert that Object into a char:

char temp=(Character)s.pop();

Here you turn it into a Character object and java automatically will turn it into a char primitive type.

If you are using an older version of java, you might want to try this:

char temp = ( (Character)s.pop() ).charValue();

Next time you might want to look the API of the classes you are using:

Stack
Character

anybody out there please help me out fix these bug in my code. i really couldnt figure out what's wrong with my code.

input should be in a string, and the output should tell wether its a palindrome or not

please!

import java.lang.*;
import java.util.*;
import java.io.*;

public class istak
{
    public static void main(String[] args)throws IOException
    {

	InputStreamReader wee= new InputStreamReader(System.in);
	BufferedReader stndin= new BufferedReader(wee);	

	    String word;
	    Stack s= new Stack();
	    int i,count=0;	
	
	    System.out.print("\nInput string: ");
	    word=stndin.readLine();
	    char array[]=word.toCharArray();	     

	

	for(i=0;i<array.length;i++)
	{
	 char val=array[i];
	   s.push(val);
	}

	for(i=0;i<array.length/2;i++)
	{
	char nxt=array[i];
	char temp=s.pop();
		if(nxt==temp)
		{
		 count++;
		}	   
	}


	if(count==array.length/2)
	{
	    System.out.println("PALINDROME");
        }

        else
        {
           System.out.println("NOT PALINDROME");
        }
	
   }

}

>>hmm.. i hve sum codes hre.. bt its n0t using buffrd readr.. bt mybe 8 myt be a hlp 2 u.. ^__^


/* Write a program to find whether no. is palindrome or not.

Example :

Input - 12521 is a palindrome no.

Input - 12345 is not a palindrome no. */

class Palindrome{

public static void main(String args[]){

int num = Integer.parseInt(args[0]);

int n = num; //used at last time check

int reverse=0,remainder;

while(num > 0){

remainder = num % 10;

reverse = reverse * 10 + remainder;

num = num / 10;

}

if(reverse == n)

System.out.println(n+" is a Palindrome Number");

else

System.out.println(n+" is not a Palindrome Number");

}

}

commented: Read rules when you join forum! No chat/l337 language and use of code tags expected here. -3
commented: learn how to write 3ngl1sh, and contribute, don't give out lousy code if a better alternative has already been handed -1

egee, your code is much better. Just correct the line I told you. I usually use another algorithm but your version is also interested.
I would suggest to use your code.

First of all you need to tell us what of mistakes your are having.
Is it compiler or it gives you wrong results and what kind.

We might not be in front a computer with java installed.

As for your error:

The pop method doesn't return a char value but an Object.

Stack.pop() returns an Object. And you are trying to put that value into a char. Since you know that that value is actually a char, you need to convert that Object into a char:

char temp=(Character)s.pop();

Here you turn it into a Character object and java automatically will turn it into a char primitive type.

If you are using an older version of java, you might want to try this:

char temp = ( (Character)s.pop() ).charValue();

Next time you might want to look the API of the classes you are using:

Stack
Character

>>thanks! it did help alot!

hmm.. i hve sum codes hre.. bt its n0t using buffrd readr.. bt mybe 8 myt be a hlp 2 u.. ^__^

/* Write a program to find whether no. is palindrome or not.

   Example :

           Input - 12521 is a palindrome no.

           Input - 12345 is not a palindrome no. */

class Palindrome{

      public static void main(String args[]){

          int num = Integer.parseInt(args[0]);

          int n = num; //used at last time check

          int reverse=0,remainder;

          while(num > 0){

                remainder = num % 10;

                reverse = reverse * 10 + remainder;

                num = num / 10;

           }

          if(reverse == n)

              System.out.println(n+" is a Palindrome Number");

          else

              System.out.println(n+" is not a Palindrome Number");

     }

} 

end quote.

thanks mayo! thanks to all who helped!

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.