//*************************************** // Program Name: calcsoundex.java * // Author: Pat Moss * // Date: Nov 12, 2005 Rev. 02 * // * // Demonstrate class "calcsoundex", etc.* //*************************************** // Receive an input data string from the user, and // Generate an equivalent 4-character Soundex code // of the form A123 // Soundex Code Examples include: // Washington W252 // Wu W000 // DeSmet D253 // Gutierrez G362 // Pfister P236 // Jackson J250 // Tymczak T522 // Ashcraft A261 (or A226) // Hammer H560 // Moss M200 // Phoenix P520 // Reference URLs: // www.highprogrammer.com/alan/numbers/soundex.html // www.zynetwc.com/~storm/sounde~1.htm import java.util.Scanner; public class calcsoundex { private final String ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; private final String SDXCODES = "01230120022455012623010202"; private String globalinpstr = ""; // user input data string private int globalinplen = 0; // user input data string length private int globalidx = -1; // Current user input data string processing index Scanner scan = new Scanner(System.in); // constructor calcsoundex() { initall(); } // initialize all variables void initall() { globalinplen = 0; } // process the current user input string public void getUserData() { char codeprev = '-'; // Previous Soundex code char codecurr = ' '; // Current Soundex code char nextchar = ' '; // Next character from user input string String outstr = ""; // Soundex code output string int outlen = 0; // Soundex code output string length System.out.print ("Enter input string ('*' to Exit): "); globalinpstr = scan.nextLine().toUpperCase() + " "; // while #1: test for Exit sentinel value '*' while (globalinpstr.charAt(0) != '*') { // initialize variables initall(); globalinplen = globalinpstr.length(); globalidx = -1; outstr = ""; outlen = 0; codeprev = '-'; codecurr = ' '; nextchar = ' '; // loop until we have a 4-character Soundex code while (outlen < 4) // while #2 { nextchar = getChar(); // get next U.C. user input data char "A" to "Z" if (nextchar == '*') // test for EOF sentinel value present { // yes: EOF sentinel value is present if (outlen == 0) // test for no valid user input data present { outlen++; // no valid user input data was present, outstr += '-'; // so provide a leading dummy character '-' } outlen++; // EOF sentinel is present, so fill with a '0' outstr += '0'; } else { // else #1 // valid data is present // normal code processing entrance codeprev = codecurr; // save prev char and use to elim dupls codecurr = getCode(nextchar); // get equivalent Soundex code character if (codecurr != codeprev) // if #1: bypass adjacent duplicate char codes { // first character is alpha, and other chars are non-zero Soundex codes if (outlen == 0) { // if #2 outlen++; outstr += nextchar; // first character is alpha } else { if (codecurr != '0') { // if #3: // accept only non-zero Soundex code chars outlen++; outstr += codecurr; } // if #3 } // if #2 } // if #1 } // else #1 } // while #2 System.out.print ("The Soundex Code is: "); System.out.println (outstr); System.out.print ("Enter input string ('*' to Exit): "); globalinpstr = scan.nextLine().toUpperCase() + " "; } // while #1 } // fetch the next alphabetic input character private char getChar() { while (globalidx < globalinplen) // while #1 { globalidx++; if (globalidx >= globalinplen) return ('*'); // reached end of user input data string // so return an EOF sentinel value '*' // fetch the next user input data character char ch = globalinpstr.charAt(globalidx); // if the next input data character is alphabetic, then return it // otherwise keep looping if ((ch >= 'A') && (ch <= 'Z')) return (ch); } // while #1 return ('*'); // reached end of user input data string // so return an EOF sentinel value '*' } // convert current U.C. alphabetic user input data char to a Soundex code private char getCode(char inpchar) { for (int i=0; i<26; i++) // for #1 { if (inpchar == ALPHABET.charAt(i)) // if #1 return (SDXCODES.charAt(i)); } // if #1 return '-'; // error condition (should not occur) } // for #1 }