Java troubles part 2

FINISHED!

My God, that was hell.

In case anyone was wondering what the heck I was supposed to be doing with that, the question was:

“Often on a Web page, the user is asked to supply personal information, such as a telephone number.  Your program should take an input from the keyboard representing a telephone number.  We will consider that the input is a valid telephone number if it contains exactly 10 digits and any number of dash (-) and whitespace characters.  Keep prompting the user for a telephone number until the user gives you a valid one.  Once you have a valid telephone number, you should assume that the digits (only the digits, not the hyphen[s] nor the whitespace) in the telephone number may have been encrypted by shifting each number by a constant value.  For instance, if the shift is 2, a 0 becomes a 2, a 1 becomes a 3, a 2 becomes a 4, …, an 8 becomes a 0, and a 9 becomes a 1.  However, we know that the user is from New York, where the decrypted area code (after the shift is applied), represented by the first three digits of the input, is 212.  Your program needs to decrypt the telephone number and output the decrypted telephone number with the format 212-xxx-xxxx, as well as the shift value of the encrpytion.  If there was an error in the input and the area code cannot be decrpted to 212, you should output that information.

/*
Program name: PhoneNumberDecrypt.java
Author: Robert Zygala
Date: 11/15/2005
Assignment: Java 5 Illuminated page 363 # 72

*/


import java.util.Scanner;

public class PhoneNumberDecrypt
{
 
 
  public static void main( String [] args )
  {
 
/////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////First, declare all of the variables/////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////
    boolean validPhoneNumber;
 
    int digitsInNumber, count, i, phoneNumberLength;
   
  String phoneNumber, decryptedPhoneNumber = “”, decryptedAreaCode = “”, decryptedPrefix = “”,
         decryptedSuffix = “”;
    
  Scanner scan = new Scanner( System.in );
  
  char nextCharacter, decrypting;
 
 
/////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////
 
 
    do  // while( !(validPhoneNumber) )
  {
//////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////Next, get the input encrypted phone number//////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////

    digitsInNumber = 0;
  count = 0;
 
      System.out.print( “Please enter the encrypted phone number: “);    
  
  phoneNumber = scan.nextLine();
   
/////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////Next, make sure there are 10 digits in the string//////////////////////////////////
/////////////////////      and gets phone number again if not         //////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////


 
   while( digitsInNumber != 10 )
 {
   phoneNumberLength = phoneNumber.length();
   count = 0;
  
   while( count < phoneNumberLength  ) //counts number of digits in phoneNumber string
   {//begin while
       nextCharacter = phoneNumber.charAt( count ); 
 
   switch(nextCharacter)
   {//begin switch
     case ‘0’:   
   case ‘1’:
   case ‘2’:       
   case ‘3’:       
   case ‘4’:        
   case ‘5’:       
   case ‘6’:        
   case ‘7’:         
   case ‘8’:       
   case ‘9’:
     digitsInNumber++;
     break;
       
   default:
    break;  
           
     } //end switch( nextCharacter)
  
   count++;
  
    }//end while( count <  phoneNumberlength ) Loop

  if(digitsInNumber != 10 )
  {
    digitsInNumber = 0;
      System.out.print( “Valid phone numbers have 10 digits.  Please try again: ” );
  phoneNumber = scan.nextLine();
  }
 
   }

////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////

  
////////////////////////////////////////////////////////////////////////////////////////////////////////
///////This next section throws away all non-digit characters and “decrypts” digit characters///////////  
////////////////////////////////////////////////////////////////////////////////////////////////////////
      phoneNumberLength = phoneNumber.length();  
  i = 0; //set index to 0 for scanning
  
  while( decryptedPhoneNumber.length() < digitsInNumber )
  {
    
   nextCharacter = phoneNumber.charAt( i );
  
   switch(nextCharacter)
   {
     case ‘0’:
     decrypting = ‘8’;
     decryptedPhoneNumber += decrypting; //adds the newly decrypted digit to decryptedPhoneNumber
     break;
   
   case ‘1’:
     decrypting = ‘9’;
           decryptedPhoneNumber += decrypting; //adds the newly decrypted digit to decryptedPhoneNumber
     break;
   
   case ‘2’:
     decrypting = ‘0’;
     decryptedPhoneNumber += decrypting; //adds the newly decrypted digit to decryptedPhoneNumber
         break;
    
   case ‘3’:
     decrypting = ‘1’;
     decryptedPhoneNumber += decrypting; //adds the newly decrypted digit to decryptedPhoneNumber
     break;
   
   case ‘4’:
     decrypting = ‘2’;
     decryptedPhoneNumber += decrypting; //adds the newly decrypted digit to decryptedPhoneNumber
     break;
   
   case ‘5’:
     decrypting = ‘3’;
     decryptedPhoneNumber += decrypting; //adds the newly decrypted digit to decryptedPhoneNumber
     break;
   
   case ‘6’:
     decrypting = ‘4’;
     decryptedPhoneNumber += decrypting; //adds the newly decrypted digit to decryptedPhoneNumber
           break;
   
   case ‘7’:
     decrypting = ‘5’;
     decryptedPhoneNumber += decrypting; //adds the newly decrypted digit to decryptedPhoneNumber
     break;
    
   case ‘8’:   
     decrypting = ‘6’;
     decryptedPhoneNumber += decrypting; //adds the newly decrypted digit to decryptedPhoneNumber
     break;
    
   case ‘9’:
     decrypting = ‘7’;
       decryptedPhoneNumber += decrypting; //adds the newly decrypted digit to decryptedPhoneNumber
     break;
    
   default:
    break;  
           
     } //end switch( nextCharacter)      

   i++; 
  
  }
  
////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////Next, split the decrypted phone number into it’s component pieces/////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////
   
    for( i = 0; i <= 9; i++)
  {
  
    if( i <= 2 ) //Takes the first 3 numbers in decryptedPhoneNumber and assigns them to decryptedAreaCode
    {
      decryptedAreaCode += decryptedPhoneNumber.charAt( i );
   
    }
    else if( i > 2 && i <= 5 ) //Takes the next 3 numbers in decryptedPhoneNumber and assigns them to decryptedPrefix
    {
      decryptedPrefix += decryptedPhoneNumber.charAt( i );
    }
    else //Takes the last 4 numbers in decryptedPhoneNumber and assigns them to decryptedSuffix
    {
      decryptedSuffix += decryptedPhoneNumber.charAt( i );
    }
     
  }//ends for( int counter = 0; counter < 11; counter++) loop
  

  
///////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////Finally, test to make sure the number is valid and output it properly formatted///////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////
  
  if( decryptedAreaCode.contentEquals( “212” ) )
  {
   validPhoneNumber = true;
   System.out.println(“\nThe shift value was 2, and the decrypted phone number is ” + decryptedAreaCode + “-”
                        + decryptedPrefix + “-” + decryptedSuffix );
  }
  else
  {
   validPhoneNumber = false;
   System.out.println(“\nThe decrypted phone number is ” + decryptedAreaCode + “-”
                        + decryptedPrefix + “-” + decryptedSuffix );
   System.out.println( “\nThe decrypted number does not have an area code of 212, \nso it is not a valid New York phone number\n”);
  
  //resets the decrypted variables for the next run through the loop
  decryptedPhoneNumber = “”;
  decryptedAreaCode = “”;
  decryptedPrefix = “”;
  decryptedSuffix = “”;
  }

    
  }while( !(validPhoneNumber) ); //repeats until the output is valid
 
 
  } //ends main()
  
}//ends the class



And it works!  WOOHOO!  That was a nightmare.  eesh.

No Responses to “Java troubles part 2”

  1. anonymous Says:

    I knew you could do it! Great Job!