0

I am trying to write a sorting program that prompts input of names and sorts them alphabetically, capitalizing the first letter of each name. I am just simply lost.
I am thinking about splitting the code into two methods one for casing and the other for sorting.

Here's what I got so far for the capitalization bit:

titleCase(String x)

name = x

x.toLowerCase

x.substring(0;)toUppercase

Any help would be greatly appreciated.

The end result should be something like this:

Enter the next name:

zeb

Enter the next name:

rita

Enter the next name:

SUE

Enter the next name:

adele

Enter the next name:

BarBara

Enter the next name:

StoP

[Adele, Barbara, Rita, Sue, Zeb]

  • So whats your question? We aren't just going to write some code for you. The little code you have provided isn't even valid code – user1231232141214124 Apr 08 '16 at 16:34
  • SO isn't really a place for dumping your homework assignment. What specifically are you having a problem with? Seems like you have the capitalization (somewhat) in hand. How will you collect them? A list? – Dave Newton Apr 08 '16 at 16:35
  • 1
    Possible duplicate of [Simple way to sort strings in the (case sensitive) alphabetical order](http://stackoverflow.com/questions/11176227/simple-way-to-sort-strings-in-the-case-sensitive-alphabetical-order) – rafaelbattesti Apr 08 '16 at 16:35
  • @Ferrariman11 See my answer. It should solve your capitalization issue. If it helps you please mark it as a solution. – nhouser9 Apr 08 '16 at 16:37
  • I am sorry. Maybe I should have clarified that I did not understand the alphabetical sorting part. I am trying to do this on my own with little teaching instruction. – Ferrariman11 Apr 08 '16 at 16:39
  • You are trying to do this on your own yet you are coming here asking us to show you how to do it....? – user1231232141214124 Apr 08 '16 at 16:41
  • Looking for some guidance to go forward in the right direction, not for you to write all my code. Nothing personal ~ redFIVE – Ferrariman11 Apr 08 '16 at 16:42
  • Your first step should have been to google "sorting java". This isn't some obscure undocumented concept – user1231232141214124 Apr 08 '16 at 16:43
  • Ok sorry for wasting your time @redFIVE – Ferrariman11 Apr 08 '16 at 16:47

3 Answers3

0

You can write a method to take a string and loop over each character, setting them to the appropriate value:

public String titleCase(String toEdit) {
 String output = "";
 String toAppend = "";
 for (int i = 0; i < toEdit.length; i++) {
  if (i == 0) {
   toAppend = (toEdit.charAt(i) + "").toUpperCase();
  } else {
   toAppend = (toEdit.charAt(i) + "").toUpperCase();
  }
  output = output + toAppend;
 }
 return output;
}
nhouser9
  • 6,730
  • 3
  • 21
  • 42
  • Thank you for helping @nhouser9 – Ferrariman11 Apr 08 '16 at 16:48
  • @Ferrariman11 happy to help. if it answered your question it would be cool if you would mark it as an answer or upvote it to counteract the reputation i lost by getting downvoted by redFive – nhouser9 Apr 08 '16 at 16:49
0

Separating functionality is a good idea. The code snippet you wrote would fail, as is, due to syntax errors, however. It looks like you're on the right track for the casing portion.

Check out the String class in the Java API and look at the compareTo function for comparing strings against each other for sorting.

0

Upper case the first character, turn it into a String, then take the rest of the name, lower case it then concatenate it to the first character. If there's only one character then just upper case it and make it a String.

public String nameCase(String name) {
  if(name == null || name.length() == 0) {
    return name;
  }
  if(name.length() > 1) {
    return String(Character.toUpperCase(name.charAt(0))).concat(name.substring(1).toLowerCase());
  } else {
    return String(Character.toUpperCase(name.charAt(0)));
  }
}
Joels Elf
  • 714
  • 6
  • 10