Skip to main content
2 of 7
added 1 characters in body
Gandalf
  • 163
  • 1
  • 1
  • 4

making code more efficient

Hey so I wrote a program for an assignment at college and I'd like to receive some pointers on making my code a little more efficient. It'd be much appreciated. I'm still new to coding so I apologize if my code is horrendous.

/******************************************************************************

 * Program Name:          Lab07 - Palindrome Program

 * Program Description: This program determines whether a number is a Palindrome or not
 it rejects any single digit entries or negative entries.

 * Program Author:        Alexis Davalos

 * Date Created:          11/4/2013

 * Change#        Change Date      Programmer Name        Description

 * -------        ------------     -------------------    ---------------------

******************************************************************************/
package lab07;

import java.util.Scanner;
public class Lab07 {

public static int reverse(int number){
       
       int result = 0;
       while (number !=0){
           int remainder = number % 10;
           result = result * 10 + remainder;
           number = number / 10;
       }
       return result;
   }
   public static boolean isPalindrome(int input){
   
   int Palindrome = reverse(input);
   
   if (Palindrome == input){
       
       return true;          
   }
   else
       return false;
   }
    public static void main(String[] args) {
   
   int integer = 0;
   Scanner input = new Scanner(System.in);      
   System.out.print("Enter a positive, multi-digit integer: ");
   integer = input.nextInt();
   while (integer <= 9 && integer > 0)
   {
        System.out.println(integer + " is a single digit. Please re-enter another integer: ");
        integer = input.nextInt();
                if (isPalindrome(integer) && (integer > 0 && integer > 9))
           {
                System.out.println(integer + " is a palindrome");
                return;
           }
           else if (!isPalindrome(integer) && (integer > 0 && integer > 9))
           {
                System.out.println(integer + " is not a palindrome");
                return;
           }
         
   }
   while (integer < 0)
   {
       System.out.println(integer + " is negative. Please re-enter another integer: ");
       integer = input.nextInt();
                if (isPalindrome(integer) && (integer > 0 && integer > 9))
            {
                 System.out.println(integer + " is a palindrome");
                 return;
            }
            else if (!isPalindrome(integer) && (integer > 0 && integer > 9))
            {
                 System.out.println(integer + " is not a palindrome");
                 return;
            }
                 while (integer <= 9 && integer > 0)
                        {
                             System.out.println(integer + " is a single digit. Please re-enter another integer: ");
                             integer = input.nextInt();
                                     if (isPalindrome(integer) && (integer > 0 && integer > 9))
                                {
                                     System.out.println(integer + " is a palindrome");
                                     return;
                                }
                                else if (!isPalindrome(integer) && (integer > 0 && integer > 9))
                                {
                                     System.out.println(integer + " is not a palindrome");
                                     return;
                                }
                        }                   
   }
   while (integer > 9){
                if (isPalindrome(integer) && (integer > 0 && integer > 9))
                {
                     System.out.println(integer + " is a palindrome");
                     return;
                }
                else if (!isPalindrome(integer) && (integer > 0 && integer > 9))
                {
                     System.out.println(integer + " is not a palindrome");
                     return;
                }
   }
   
}
                                           
   }
  
Gandalf
  • 163
  • 1
  • 1
  • 4