2 Replies - 6963 Views - Last Post: 14 March 2011 - 08:21 PM Rate Topic: -----

#1 mistersinyster  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 14-March 11

5 digit integer palindrome

Posted 14 March 2011 - 07:49 PM

having trouble with the while loop. i'm supposed to check to make sure that the entered integer is actually a five digit number. it complies and runs fine except for the loop. thanks in advance for any help or advice...

// Lab 2: Palindrome.java
// Program tests for a palindrome
import java.util.Scanner;

public class Palindrome
{
   // checks if a 5-digit number is a palindrome
   public void checkPalindrome()
   {
      Scanner input = new Scanner( System.in );
      
      int number; // user input number
      int digit1; // first digit
      int digit2; // second digit
	  int digit3;
      int digit4; // fourth digit
      int digit5; // fifth digit
      int digits; // number of digits in input

      number = 0;
      digits = 0;

      /* Write code that inputs a five-digit number. Display an error message
         if the number is not five digits. Loop until a valid input is received. */
      System.out.println( "Enter five digit number: " ); // prompt
      number = input.nextInt(); // read number 
	  while (number < 10000 && number > 99999)
	  {
	     System.out.println("Incorrect input, please try again...");
		 System.out.println( "Enter five digit number: " ); // prompt
         number = input.nextInt(); // read number 
	  }
      /* Write code that separates the digits in the five digit number. Use 
         division to isolate the left-most digit in the number, use a remainder 
         calculation to remove that digit from the number. Then repeat this 
         process. */
	  
      digit1 = number / 10000;
	  digit2 = number / 1000 % 10;
      digit3 = number % 1000 / 100 % 10;
      digit4 = number % 10000 % 1000 % 100 / 10;
	  digit5 = number % 10000 % 1000 % 100 % 10;
      /* Write code that determines whether the first and last digits are 
         identical and the second and Fourth digits are identical. Output 
         whether or not the original string is a palindrome. */
      if (digit1 == digit5 && digit2 == digit4)
	      System.out.printf("%d is a palindrome", number);
	  else 
	      System.out.printf("%d is not a palindrome", number);
	  
   } // end method checkPalindrome
} // end class Palindrome



Is This A Good Question/Topic? 0
  • +

Replies To: 5 digit integer palindrome

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,910
  • Joined: 06-March 08

Re: 5 digit integer palindrome

Posted 14 March 2011 - 08:12 PM

if(number < 10000 || number > 99999)
... invalid input

not &&
Was This Post Helpful? 1
  • +
  • -

#3 mistersinyster  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 14-March 11

Re: 5 digit integer palindrome

Posted 14 March 2011 - 08:21 PM

its always something simple... thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1