Guest User

Untitled

a guest
Feb 19th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. public class Name2 { //Christopher Miller Mod 4/5 AP Computer Science
  2. private String first,middle,last;
  3. private String title; //Mr.,Mrs., etc.
  4.  
  5. public Name2 (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. 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= "Her Epicness, Queen";}
  67. }
  68. else{
  69. title = "?";
  70. }
  71.  
  72. }
  73. public String full_name(){
  74. if (middle == " ")
  75. return title + " " + first + middle + last;
  76. else
  77. return title + " " + first + " " + middle+" "+last;
  78. }
  79.  
  80. public String last_then_rest()
  81. {
  82. return last+","+first+","+middle;
  83. }
  84. public String rtn_title(){
  85. return title;
  86. }
  87.  
  88. }
Add Comment
Please, Sign In to add comment