Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Name {
- private String first, middle, last;
- private String title; // Mr., Ms., etc.
- public Name( String str, String mr_ms ) {
- check_name( str );
- check_title( mr_ms );
- }
- private void check_name( String nm ){
- nm = remove_spaces(nm);
- int sct = 0;
- for ( int c = 0; c<nm.length(); c++){
- if ( nm.charAt(c)==' ')
- sct++;
- }
- if (sct >= 2){
- for ( int x = 0; x<nm.length(); x++){
- if ( nm.charAt(x) == ' ' && first == null)
- first = nm.substring(0,x);
- else if ( nm.charAt(x) == ' ' && middle == null)
- middle = nm.substring(x+1, nm.indexOf(" ", x+1));
- else if (nm.charAt(x) == ' ' && last == null)
- last = nm.substring(x+1);
- }
- }
- else if ( sct ==1) {
- int space = nm.indexOf(" ");
- first = nm.substring(0, space);
- middle = " ";
- last = nm.substring(space+1);
- }
- else
- first = nm;
- }
- private String remove_spaces( String s ){
- s= s.trim();
- String str = " ";
- for ( int x = 0; x<s.length();x++){
- if ( s.substring(x,x+1) != " ")
- str += s.substring(x,x+1);
- if ( s.substring(x,x+1) == " " && s.substring(x-1,x) != " ")
- str+= s.substring(x,x+1);
- }
- return str;
- }
- private void check_title( String t ){
- t = t.toLowerCase();
- 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 Epicness, King";}
- else if(t.equals("queen")){
- title= "His Epicness, 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;
- }
- }
Add Comment
Please, Sign In to add comment