Guest User

Untitled

a guest
Feb 19th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. public class Name {
  2.  
  3. private String first, middle, last;
  4. private String title; // Mr., Ms., etc.
  5.  
  6. public Name( String str, String mr_ms ) {
  7.  
  8. check_name( str );
  9. check_title( mr_ms );
  10.  
  11. }
  12.  
  13. private void check_name( String nm ){
  14. nm = remove_spaces(nm);
  15. int sct = 0;
  16. for ( int c = 0; c<nm.length(); c++){
  17. if ( nm.charAt(c)==' ')
  18. sct++;
  19. }
  20. if (sct >= 2){
  21. for ( int x = 0; x<nm.length(); x++){
  22. if ( nm.charAt(x) == ' ' && first == null)
  23. first = nm.substring(0,x);
  24. else if ( nm.charAt(x) == ' ' && middle == null)
  25. middle = nm.substring(x+1, nm.indexOf(" ", x+1));
  26. else if (nm.charAt(x) == ' ' && last == null)
  27. last = nm.substring(x+1);
  28. }
  29. }
  30. else if ( sct ==1) {
  31. int space = nm.indexOf(" ");
  32. first = nm.substring(0, space);
  33. middle = " ";
  34. last = nm.substring(space+1);
  35. }
  36. else
  37. first = nm;
  38. }
  39.  
  40. private String remove_spaces( String s ){
  41. s= s.trim();
  42. String str = " ";
  43. for ( int x = 0; x<s.length();x++){
  44. if ( s.substring(x,x+1) != " ")
  45. str += s.substring(x,x+1);
  46. if ( s.substring(x,x+1) == " " && s.substring(x-1,x) != " ")
  47. str+= s.substring(x,x+1);
  48. }
  49. return str;
  50. }
  51.  
  52. private void check_title( String t ){
  53. t = t.toLowerCase();
  54. if (t.equals("mr")||t.equals("mrs")||t.equals("ms")||t.equals("miss")||t.equals("king")||t.equals("queen")){
  55. if(t.equals("mr")){
  56. title= "Mr.";}
  57. else if(t.equals("mrs")){
  58. title= "Mrs.";}
  59. else if(t.equals("ms")){
  60. title= "Ms.";}
  61. else if(t.equals("miss")){
  62. title= "Miss.";}
  63. else if(t.equals("king")){
  64. title= "His Epicness, King";}
  65. else if(t.equals("queen")){
  66. title= "His Epicness, Queen";}
  67.  
  68. }
  69. else{
  70. title = "?";
  71. }
  72. }
  73.  
  74. public String full_name(){
  75. if (middle == " ")
  76. return first + middle + last;
  77. else
  78. return first + " "+ middle+" "+last;
  79. }
  80.  
  81. public String last_then_rest(){
  82. return last + ", " + first + " " + middle;
  83. }
  84. }
Add Comment
Please, Sign In to add comment