Guest User

Untitled

a guest
Feb 19th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. public class Name { //Christopher Miller Mod 4/5 AP Computer Science
  2. private String first,middle,last;
  3. private String title; //Mr.,Mrs., etc.
  4.  
  5. public Name (String str, String mr_ms){
  6. check_name(str);
  7. check_title(mr_ms);
  8. }
  9.  
  10.  
  11. private void check_name (String str){
  12. str = remove_spaces(str);
  13. int spc = 0;
  14. for (int i = 0; i <str.length(); i++){
  15. if(str.charAt(i)==' '){
  16. spc++;}
  17. if (spc>=2){
  18. for (int e = 0; e< str.length(); e++){
  19. if (str.charAt(e) == ' ' && first == null){
  20. first = str.substring(0,e);}
  21. else if(str.charAt(e) == ' ' && middle == null){
  22. middle = str.substring(e+1, str.indexOf(" ", e+1));}
  23. else if(str.charAt(e) == ' ' && last == null){
  24. last = str.substring(e+1);}
  25. }
  26. }
  27. else if (spc == 1){
  28. int space = str.indexOf(" ");
  29. first = str.substring(0, space);
  30. last = str.substring(space+1);
  31. middle = "";
  32. }
  33. }
  34. }
  35.  
  36. private String remove_spaces(String s){
  37. String str = " ";
  38. s.trim();
  39. for (int i = 0; i<s.length(); i++){
  40. if (s.substring(i, i+1) != " " ){
  41. str+=s.substring(i, i+1);
  42. }
  43. if (s.substring (i, i+1) == " " && s.substring (i-1, i)!= " "){
  44. str+= s.substring (i, i+1);
  45. }
  46.  
  47. if (str.charAt(0) == ' ' ){
  48. str = str.substring(1);}
  49. }
  50. return str;
  51. }
  52. private void check_title(String t){
  53. t = t.toLowerCase();
  54. String title ="";
  55. if (t.equals("mr")||t.equals("mrs")||t.equals("ms")||t.equals("miss")||t.equals("king")||t.equals("queen")){
  56. if(t.equals("mr")){
  57. title= "Mr.";}
  58. else if(t.equals("mrs")){
  59. title= "Mrs.";}
  60. else if(t.equals("ms")){
  61. title= "Ms.";}
  62. else if(t.equals("miss")){
  63. title= "Miss.";}
  64. else if(t.equals("king")){
  65. title= "His Highness, King ";}
  66. else if(t.equals("queen")){
  67. title= "Her Highness, Queen ";}
  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. {
  83. return last+","+first+","+middle;
  84. }
  85. public String rtn_title(){
  86. return title;
  87. }
  88.  
  89. }
Add Comment
Please, Sign In to add comment