Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am very good at c++. and recently i am learning java .
but i am stuck here and dont know how the program always gives me that the string is palindrome.

C#
//here goes my code
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
import java.util.Collections;
public class Test
{
	public static void display(ArrayList<Character> arr)
	{
		for(int i=0 ; i<arr.size() ; i++)
			System.out.print(arr.get(i) + " " );
		System.out.println();
	}
	public static boolean Check(ArrayList<Character> arr )
	{
		ArrayList<Character> temp = new ArrayList<Character>();
		temp = arr;
		Collections.reverse(temp);
		if(arr.equals(temp))
			return true;
		else
			return false;
	}
	public static void main(String[] args)
	{
		Scanner input = new Scanner(System.in);
		String name;
		System.out.println("enter the string");
		name = input.next();
		System.out.println(name.length());
		ArrayList<Character> arr = new ArrayList<Character>();
		for(int i=0 ; i<name.length() ; i++)
		{
			arr.add(name.charAt(i));
		}
		// it is always showing me that it is palindrome
		if(Check(arr))
			System.out.println("it is a palindrome");
		else
			System.out.println("it is not a palindrome");
	}
}


What I have tried:

MUST READ:

See, i know that there are lot better ways to do this silly thing.So please don't suggest me a better implementation . I would be very thankful if you can find a error in this code.

Thanks.
Posted
Updated 30-Jun-16 13:39pm
Comments
ZurdoDev 30-Jun-16 16:26pm    
Just use the debugger to see what is happening.
phil.o 30-Jun-16 18:35pm    
I'm failing in understanding how one can be "very good at c++" without knowing how to debug a simple algorithm.

1 solution

In your boolean check function you are assigning arr to temp. arr and temp are same objects. If you reverse temp, you are reversing arr. You better look at how to clone to an object, not the assignment operator
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900