Search code examples
javareversepalindrome

My code to determine 3 digit palindrome infinitely asking for number


I have to make a code that determines if a three digit number is a palindrome (ie 121 where front to back it is the same,) and for some reason the program is infinitely asking for input. I cannot seem to figure out why. Any help would be appreciated, thanks!

Code:

/* Class:         CS1301
 * Section:       9:30
 * Term:          Fall 2015
 * Name:          Matthew Woolridge
 * Instructor:    Mr. Robert Thorsen
 * Assignment:    3
 * Program:       4
 * ProgramName:   PalindromeNumber
 * Purpose:       To tell the user if a number is palindrome
 * Operation:     The information to be numbers are statically instantiated in the code and
 *                the table is output to the screen.
 * Input(s):      The user inputs a three digit number
 * Output(s):     The output is whether or not the number is palindrome
 * Methodology:   This program will use selections, math statements and print to determine and tell the user if a number is palindrome
 *
 */

import java.util.Scanner;
public class PalindromeNumber
{

   public static void main (String[] main)
   {

   /******************************************************************************
                             Declarations Section                               
   ******************************************************************************/
    /****************************CONSTANTS********************************/

     // Defined variables, must be integers to get a remainder
      int reverse=0;
      int palindrome;
      int remainder;
      int num=0;

      Scanner scan = new Scanner (System.in); //Scanner utility initialization

   /******************************************************************************
                                 Inputs Section                                  
    ******************************************************************************/

      System.out.print("Please input a three digit number: ");
      palindrome = scan.nextInt(); // The following lines prompt the user to input a three digit number  

   /****************************variables********************************/ 
    // Variables are not in this section // 

      /******************************************************************************
                                 Processing Section                              
      ******************************************************************************/

      while (palindrome > 0)
      {
         remainder = palindrome % 10; //Uses the remainder function to find the remainder of the number using 10
         reverse = reverse * 10 + remainder; // Calculates the reverse by multiplying the initial reverse value of 0 by 10 and dividing by ten to get the number of reverse
         num = palindrome / 10; // Divides the palindrome by ten to get the number minus the remainder
      }

      if (num == reverse) // If the num (copied from palindrome) is equal to the reverse the number is a palindrome
      {
         System.out.println(palindrome + "  is a palindrome.");
      }
      else 
      {
         System.out.println(palindrome + "  is not a palindrome.");   // If and else statements determine if palindrome = reverse statement is true or false and print accordingly.
      } // Closes while and if statement
   /******************************************************************************
                                 Outputs Section                                
   ******************************************************************************/
        //Outputs are in processing//

   } // Ends string
} // Ends program

Solution

  • and for some reason the program is infinitely asking for input. I cannot seem to figure out why

    If it still prompts for input even though an input is already given, it is caused by the newline buffer.

    Try changing:

    palindrome = scan.nextInt(); 
    

    To:

    palindrome = Integer.parseInt(scan.nextLine()); 
    

    By the way, if it is a 3-digit input. Checking whether it is a palindrome is as easy as:

    public boolean is3Digit_Palindrome(int num){
        return (num /100) == (num % 10);     //Check first digit == last digit
    }