Skip to content

Instantly share code, notes, and snippets.

@lbattaglioli2000
Created September 18, 2020 02:32
Show Gist options
  • Save lbattaglioli2000/c50d07550bf6daaa533ae23481fe4df3 to your computer and use it in GitHub Desktop.
Save lbattaglioli2000/c50d07550bf6daaa533ae23481fe4df3 to your computer and use it in GitHub Desktop.
for kory baby <3
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.DecimalFormat;
import java.util.StringTokenizer;
public class Palindrome {
public static void main(String[] args) throws IOException {
// Setup a new BufferedReader to read user input.
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// Prompt the user to enter a phone number.
System.out.println("Enter a phone number:");
// Create a new StringBuffer for the inputted phone number.
String inputtedPhoneNumber = br.readLine();
// Clean the phone number, removing any punctuation, or white space.
String cleanedPhoneNumber = cleanPhoneNumber(inputtedPhoneNumber);
// Display the cleaned phone number.
System.out.println("Cleaned phone number: " + cleanedPhoneNumber);
// Display whether or not the phone number is a palindrome.
System.out.println("Is a palindrome: " + isPalindrome(cleanedPhoneNumber));
// Lastly, output the number in comma form.
System.out.println("Phone number in comma form: " + convertToCommaForm(cleanedPhoneNumber));
}
/**
* cleanPhoneNumber
*
* Accepts a StringBuffer dirtyPhoneNumber and sanitizes it
* to be a String of digits with no punctuation.
*
* @param dirtyPhoneNumber
* @return String
*/
public static String cleanPhoneNumber(String dirtyPhoneNumber){
// Create a new StringTokenizer to break the dirtyPhoneNumber string apart into tokens.
StringTokenizer tokenizer = new StringTokenizer(dirtyPhoneNumber, " ");
// First, extract the area code from the first token.
StringBuffer areaCode = new StringBuffer(tokenizer.nextToken())
.deleteCharAt(0)
.deleteCharAt(3);
// Get the second token, which is the remaining part of the phone number.
String phoneNumber = tokenizer.nextToken();
// Delete the '-' at position 3, again we know it's position 3 since the phone
// numbers are all inputted in the same fashion.
tokenizer = new StringTokenizer(phoneNumber, "-");
String cleanPhoneNumber = tokenizer.nextToken() + tokenizer.nextToken();
// Return the cleaned area code, concatenated with the remaining phone number
return areaCode + cleanPhoneNumber;
}
/**
* isPalindrome
*
* Determines whether or not the inputted String, cleanedPhoneNumber, is
* a palindrome or not.
*
* @param cleanedPhoneNumber
* @return boolean
*/
public static boolean isPalindrome(String cleanedPhoneNumber){
// Create an empty StringBuffer object to store the reversed string
StringBuffer reversedPhoneNumber = new StringBuffer(cleanedPhoneNumber.length());
// Reverse the string
for(int i = 1; i <= cleanedPhoneNumber.length(); i++){
reversedPhoneNumber.append(cleanedPhoneNumber.charAt(cleanedPhoneNumber.length() - i));
}
// Return the result of a call to the equals method on the String class, to see if these two
// phone numbers are equal.
return reversedPhoneNumber.toString().equals(cleanedPhoneNumber);
}
/**
* convertToCommaForm
*
* Converts the inputted String, cleanedPhoneNumber, to be a long, comma
* separated number.
*
* @param cleanedPhoneNumber
* @return
*/
public static String convertToCommaForm(String cleanedPhoneNumber){
// Parse the cleanedPhoneNumber String for a long number.
long number = Long.parseLong(cleanedPhoneNumber);
// Create a new DecimalFormatter object to format the long number,
// matching the pattern specified to the constructor.
DecimalFormat decimalFormatter = new DecimalFormat("#,###,###,###");
// Return the formatted number from the DecimalFormatter
return decimalFormatter.format(number);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment