Results 1 to 6 of 6
  1. #1
    thaotic is offline Member
    Join Date
    Mar 2012
    Posts
    3
    Rep Power
    0

    Default Problem with Palindromes

    I have recently started using java and decided to try to make a palindrome program (a palindrome is where the number is the same forwards and backwards).

    so far I have managed to find the inverse, but the program wont terminate when it has found the inverse, and has some strange outputs. I dont know much about java so i've probably overlooked some simple things.

    import java.util.*;
    import java.lang.Math;
    Java Code:
    public class Palindrome
    {
    	public static void main(String[] args)
    	{
    		Scanner input = new Scanner(System.in);
    		
    		System.out.println("Input Initial Value: ");
    		int numPrimary = input.nextInt(); 		//Takes your initial input
    		int numInverse = 0;						//The variable that will become the inverse of the 'primary'
    		int numTemp = numPrimary;				//temporary variable
    		int[] remainderArray;					//array for storing the remainders
    		int[] tempArray;						//temporary array for flipping the order of the remainders
    		int runCount = 0;							//for counting how many times it takes to find a Palindrome
    		int arrayCount = 0;						//a count used for the arrays
    		int primaryLength;						//the length of the primary variable, used for array sizes
    		int tempCount = 0;						//a temporary count for inverting the remainders
    		int mathTemp;							//a temporary variable for creating the numInverse
    		boolean numCheck;						//for checking if is a palindrome
    
    		do
    		{		
    			numPrimary = numPrimary + numInverse;
    			arrayCount = 0;
    			tempCount = 0;
    			
    			//Setting size of the arrays
    			primaryLength = Integer.toString(numPrimary).length(); 
    			remainderArray = new int[primaryLength+1];
    			tempArray = new int[primaryLength+1];
    			
    			//finds the remainders of the primary variable
    			do
    			{
    				remainderArray[arrayCount] = numTemp % 10;
    				numTemp = numTemp / 10;	
    				arrayCount = arrayCount + 1;				
    			}
    			while(numTemp>0);
    			
    			//sets an array with the elements flipped around
    			do
    			{
    				tempArray[arrayCount] = remainderArray[tempCount];
    				tempCount = tempCount + 1;
    				arrayCount = arrayCount - 1;
    			}
    			while(arrayCount >0);
    			
    			//uses the flipped array to find the inverse
    			do
    			{
    				numTemp = numInverse;
    				mathTemp = (int)Math.pow(10, arrayCount);
    				numInverse = numTemp + (tempArray[arrayCount]*mathTemp);
    				arrayCount = arrayCount + 1;
    			}
    			while(arrayCount<=primaryLength);
    			
    			//recalibrates the inverse and adds the the run counter
    			numInverse = numInverse / 10;
    			runCount = runCount + 1;
    			System.out.println(numInverse);
    		}while (numPrimary != numInverse);
    	}
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default Re: Problem with Palindromes

    What output do you get and what output do you expect?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    thaotic is offline Member
    Join Date
    Mar 2012
    Posts
    3
    Rep Power
    0

    Default Re: Problem with Palindromes

    It depends on the input.
    ie. Input:12
    Expected Output: 21
    33
    Actual Output: 21
    3
    13
    34
    37
    50
    710
    72
    34
    246
    488
    6932
    8932
    3132
    22626
    28524
    67110
    46828
    87546
    873332
    710670
    2147084
    695449
    5014140

  4. #4
    berb12 is offline Member
    Join Date
    Nov 2011
    Posts
    21
    Rep Power
    0

    Default Re: Problem with Palindromes

    An easy way I made a Palindrome program was using strings rather than integers. You can make the input number a string, and add each digit of the number string to an array using the substring method. Then all you need to do is make a backwards number array that adds the substrings in the opposite direction. Then you can compare the forwards and backward arrays. If all of the elements match up in order (forward[1] = backward[1].....) , it is a palindrome.

  5. #5
    thaotic is offline Member
    Join Date
    Mar 2012
    Posts
    3
    Rep Power
    0

    Default Re: Problem with Palindromes

    Java Code:
    import java.util.Scanner;
    import java.lang.Math;
    
    public class Palindromev2
    {
    	public static void main(String[] args)
    	{
    		Scanner sc = new Scanner(System.in);
    		int valPrimary;
    		int valInverse;
    		int valLength;
    		int valTemp;
    		int[] aryRemainder;
    		int cntArray;
    		int cntMath;
    		int cntRuns;
    		
    		
    		System.out.print("Input initial Integer: ");
    		valPrimary = sc.nextInt();
    		
    		do
    		{
    			valInverse = 0;
    			valLength = Integer.toString(valPrimary).length();
    			cntArray = valLength - 1;
    			cntMath = 0;
    			cntRuns = 0;
    			valTemp = valPrimary;
    			
    			aryRemainder = new int[valLength];
    			
    			do
    			{
    				aryRemainder[cntArray] = valTemp % 10;
    				valTemp= valTemp / 10;
    				cntArray = cntArray - 1;
    			}
    			while(cntArray>=0);
    				
    			
    			do
    			{
    				valInverse = valInverse + (aryRemainder[cntMath]*((int)Math.pow(10,cntMath)));
    				cntMath = cntMath + 1;
    			}
    			while(cntMath < valLength);
    				
    			cntRuns = cntRuns + 1;
    			System.out.println("The Inverse is: " + valInverse);
    			System.out.println("");
    			
    			if (valInverse=valPrimary);
    			{
    				break;
    			}
    			
    			valPrimary = valPrimary + valInverse;
    			System.out.println("The new Primary is : " + valPrimary);			
    		}
    		while(valLength>0);
    		
    		System.out.println("The final Palindrome is: " + valPrimary);
    		System.out.println("It took " + cntRuns + " to find a Palindrome");
    	}
    }
    This is what i've come up with now, but the 'if' statement doesn't work. I don't understand why it doesn't, can anyone explain it to me?

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default Re: Problem with Palindromes

    Java Code:
    if (valInverse=valPrimary);
    First, that's an assignment '=' not a comparison '=='.
    Second, look at the semi-colon...
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Counting number of palindromes in a file
    By vuzuggu in forum New To Java
    Replies: 3
    Last Post: 10-20-2011, 05:52 PM
  2. Project Euler #4 - Palindromes
    By Awk34 in forum New To Java
    Replies: 12
    Last Post: 12-19-2010, 10:28 AM
  3. Finding out number of Palindromes in a Sentence
    By rameshiit19 in forum New To Java
    Replies: 1
    Last Post: 09-13-2010, 03:44 AM
  4. Find All Palindromes
    By spleenlol in forum New To Java
    Replies: 6
    Last Post: 02-04-2010, 02:33 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •