Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. package soundex;
  2.  
  3. public class Soundex {
  4. private String w;
  5. private char ant;
  6.  
  7. private static void main(String[] args) {
  8. Soundex sdx = new doSoundex();
  9. String s = new String();
  10. s = JOptionPane.showInputDialog("Insira o texto:");
  11. sdx.setW(s);
  12. s = "Soundex: " + sdx.doSoundex();
  13. JOptionPane.showMessageDialog(null, s);
  14. }
  15.  
  16. private int checkVogal(char c) {
  17. if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
  18. return 1;
  19. else
  20. return 0;
  21. }
  22.  
  23. private char replaceSoundex(char c) {
  24. char d = 0;
  25. if (c == ant)
  26. d = '0';
  27. else {
  28. if (c == 'b' || c == 'p' || c == 'f' || c == 'v')
  29. d = '1';
  30. if (c == 'c' || c == 's' || c == 'g' || c == 'j' || c == 'k' || c == 'q' || c == 'x' || c == 'z')
  31. d = '2';
  32. if (c == 'd' || c == 't')
  33. d = '3';
  34. if (c == 'l')
  35. d = '4';
  36. if (c == 'm' || c == 'n')
  37. d = '5';
  38. if (c == 'r')
  39. d = '6';
  40. if (c == 'h' || c == 'w' || c == 'y')
  41. d = '0';
  42. }
  43. ant = c;
  44. return d;
  45. }
  46.  
  47. public void setW(String s) {
  48. w = s;
  49. }
  50.  
  51. public String Soundex::doSoundex() {
  52. char temp[] = new char[4];
  53. int count = 1;
  54. int i = 0;
  55. temp[0] = w.charAt(0);
  56. for (i = 1; i < w.length() || count <= 3; i++) {
  57. if (checkVogal(w.charAt(i)) == 1) ;
  58. else {
  59. temp[count] = replaceSoundex(w.charAt(i));
  60. count++;
  61. }
  62. }
  63. if (count < 4) {
  64. for (i = count; i < 4; i++)
  65. temp[i] = 0;
  66. }
  67. return temp;
  68. }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement