I'm creating a program for school that tells whether or not a phone number is a palindrome. A palindrome is when a number is reversed and the order is the same. The program is below, but the problem is that the program that does not print right. In the last method it's supposed to return a true or false to the main function, but no matter what number is entered it always goes to the false path and if the true and false were to be reversed then it would print everything as being true even when it shouldn't be. Let me know if I need to clarify anything. Thank You.
Here's the code:
import java.io.*;
import java.util.StringTokenizer;
public class palindrome
{
public static void main(String[] args) throws IOException
{
String phone;
BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
System.out.print("Enter a telephone number (xxx) xxx-xxxx: ");
phone = br.readLine();
phone = remove(phone);
System.out.println("Original number: " + phone);
if (reverse(phone))
System.out.println("This is a palindrome");
else
System.out.println("This is not a palindrome");
}
public static String remove(String telephone)
{
StringTokenizer num;
StringBuffer numbers, numbers2, numbers3;
StringBuffer allnumbers = new StringBuffer();
num = new StringTokenizer(telephone, " ()-");
numbers = new StringBuffer(num.nextToken());
numbers2 = new StringBuffer(num.nextToken());
numbers3 = new StringBuffer(num.nextToken());
allnumbers.append(numbers).append(numbers2).append (numbers3);
telephone = allnumbers.toString();
return telephone;
}
public static boolean reverse(String palindrome)
{
StringBuffer numbers = new StringBuffer();
numbers = new StringBuffer(palindrome).reverse();
palindrome = numbers.toString();
System.out.println("palindrome: " + palindrome);
System.out.println("numbers: " + numbers);
if (palindrome.equals(numbers))
return true;
else
return false;
}
}