Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Name { //Christopher Miller Mod 4/5 AP Computer Science
- private String first,middle,last;
- private String title; //Mr.,Mrs., etc.
- public Name (String str, String mr_ms){
- check_name(str);
- check_title(mr_ms);
- }
- private void check_name (String str){
- str = remove_spaces(str);
- int spc = 0;
- for (int i = 0; i <str.length(); i++){
- if(str.charAt(i)==' '){
- spc++;}
- if (spc>=2){
- for (int e = 0; e< str.length(); e++){
- if (str.charAt(e) == ' ' && first == null){
- first = str.substring(0,e);}
- else if(str.charAt(e) == ' ' && middle == null){
- middle = str.substring(e+1, str.indexOf(" ", e+1));}
- else if(str.charAt(e) == ' ' && last == null){
- last = str.substring(e+1);}
- }
- }
- else if (spc == 1){
- int space = str.indexOf(" ");
- first = str.substring(0, space);
- last = str.substring(space+1);
- middle = "";
- }
- }
- }
- private String remove_spaces(String s){
- String str = " ";
- s.trim();
- for (int i = 0; i<s.length(); i++){
- if (s.substring(i, i+1) != " " ){
- str+=s.substring(i, i+1);
- }
- if (s.substring (i, i+1) == " " && s.substring (i-1, i)!= " "){
- str+= s.substring (i, i+1);
- }
- if (str.charAt(0) == ' ' ){
- str = str.substring(1);}
- }
- return str;
- }
- private void check_title(String t){
- t = t.toLowerCase();
- String title ="";
- if (t.equals("mr")||t.equals("mrs")||t.equals("ms")||t.equals("miss")||t.equals("king")||t.equals("queen")){
- if(t.equals("mr")){
- title= "Mr.";}
- else if(t.equals("mrs")){
- title= "Mrs.";}
- else if(t.equals("ms")){
- title= "Ms.";}
- else if(t.equals("miss")){
- title= "Miss.";}
- else if(t.equals("king")){
- title= "His Highness, King ";}
- else if(t.equals("queen")){
- title= "Her Highness, Queen ";}
- }
- else{
- title = "?";
- }
- }
- public String full_name(){
- if (middle == " ")
- return first + middle + last;
- else
- return first + " "+ middle+" "+last;
- }
- public String last_then_rest()
- {
- return last+","+first+","+middle;
- }
- public String rtn_title(){
- return title;
- }
- }
Add Comment
Please, Sign In to add comment