Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

So I'm writing a program to format names to title case and display them in alphabetic order. Any advice please? And how can i add more methods?

public static void main (String [] args)
{    
    //local variables
    string name1;
    string name2;

     myLib = new Library();

     /********************   Start main method  *****************/

     //prompt user for first Name
     System.out.print("Enter First Name : ");
     name1 = Keyboard.readChar(); 

     //prompt user for second
     System.out.print("Enter Second Name : ");
     name2 = Keyboard.readChar(); 

     //Format names to Title Case
     name1.replace(0, name1.length(), name1.toString().toLowerCase());
     name2.replace(0, name2.length(), name2.toString().toLowerCase()); 

     name1.setCharAt(0, Character.toTitleCase(name1.charAt(0)));
     name2.setCharAt(0, Character.toTitleCase(name2.charAt(0)));

     //clear the screen
     myLib.clrscr();

     //Display names in alphabet order
     if (name1.toString().compareTo(name2.toString()) >= 0) {
         System.out.println(name2 + " " + name1);    
     } else {
         System.out.println(name1 + " " + name2);
     }    

     //pause the screen
     //myLib.pause();

  } //end main method

} //end
share|improve this question
1  
Could you post the string class too? – palacsint Oct 1 '12 at 6:03

2 Answers

Usually in these cases you can implement a Comparator<String>. This way it'll be portable, and can make use of the Collections API (e.g. Collections.sort(collection, TITLE_CASE_COMPARATOR);.

private static final Comparator<String> TITLE_CASE_COMPARATOR = new Comparator<String>() {
   @Override
   public int compare(String o1, String o2) { 
     // null-safe checks
     // equal
     if (o1 == null && o2 == null) { return 0; }
     // greater than, implies o2 is null
     else if (o1 != null) { return 1; } 
     // less than, implies o1 is null
     else if (o2 != null) { return -1; } 

     // title case comparison here
   }
};

Some other ideas/methods you can also consider:

  • Check cases where title cases do not apply (just to name a few [there are more]: a, an, the, or)
  • Think about choosing the right collection, some collections take a Comparator as an argument which maintains the order for you (depending when/where you want to display the sorted results)
  • You might also consider a pretty formatting for it, such as the total of entries that were processed (maintaining a history of previous inputs)
  • Additional considerations for handling input (empty input, ignoring invalid characters, duplicate entries)
share|improve this answer

Create a List

List<String> al = new ArrayList<String();

and add (you can also use something like while(! stringRead.isEmpty()) {..} )

name = Keyboard.readChar()).toLowerCase();
al.add(name.substring(0, 1).toUpperCase() + name.substring(1));

EDIT after comment

1°) then Sort (the list is 'cloned') and print directly frome the Constructor return
This solution is recommended to protect data published in a Web page.

System.out.println(new TreeSet<String>(al).toString());

2°) The List is sorted then printed

Collections.sort(al);
System.out.println(al.toString());
share|improve this answer
References to objects should be as generic as possible (i.e. List<String> a1 is preferred over ArrayList<String> a1) – lchau Oct 1 '12 at 10:25
@lchau . Exact, thanks - A mistake I maid sometime : because Eclipse's completion is too useful, I forgot to refer to Interface by removing Array – cl-r Oct 1 '12 at 11:52
+1 and a note: Collections.sort would keep the duplicated elements in the list (unlike the TreeSet and other sets). – palacsint Oct 1 '12 at 17:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.