Results 1 to 7 of 7
  1. #1
    leoshiner is offline Member
    Join Date
    Oct 2010
    Posts
    28
    Rep Power
    0

    Default array to sort names in alphabetical order

    just trying to complete a tutorial on constructing a basic array to sort names in alphabetical order and still cannot get it to work would be grateful for any help

    package introtoarrays1;


    public class IntroToArrays1 {


    public static void main(String[] args) {

    String [] name = new String[6];

    String tempStr;


    name[0]="Bob";
    name[1]="Jenny";
    name[2]="Charlie";
    name[3]="Jerry";
    name[4]="Roberta";
    name[5]="Anita";


    for(int t=0;t<name.length-1;t++){

    for (int i = 0; i < name.length - 1; i++) {

    if (name[i].compareTo(name[1 + 1]) > 0) {
    tempStr = name[i];
    name[i] = name[i + 1];
    name[i + 1] = tempStr;
    }
    }

    }
    for(int i=0;i<name.length;i++){
    System.out.println(name[i]);
    }











    }

    }
    Last edited by leoshiner; 05-01-2011 at 01:02 PM. Reason: no explanation given

  2. #2
    ojn
    ojn is offline Senior Member
    Join Date
    Sep 2008
    Location
    Stockholm, Sweden
    Posts
    119
    Rep Power
    0

    Default

    Help with what? And don't say "How to fix it", ask a specific question. Here are some for you:

    1. Does the code compile?
    2. If so, what's the output?
    3. If it doesn't, what's the exact error messages you get and on which lines do they occur?

    Java Code:
    if (name[i].compareTo(name[1 + 1])
    looks weird. Did you mean i +1?

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    What results do you get? Also, did the tutorial give you the code, or suggest that you try it out? If you want to learn to manually sort that's fine, I would like to point out that you can call the static arrays method Arrays.sort.

  4. #4
    leoshiner is offline Member
    Join Date
    Oct 2010
    Posts
    28
    Rep Power
    0

    Default

    yeah Ojn that's exactly what the problem was,thanks very much,the program was running without error but I didn't realise because the characters are so similar
    Last edited by leoshiner; 05-01-2011 at 01:20 PM. Reason: more text

  5. #5
    leoshiner is offline Member
    Join Date
    Oct 2010
    Posts
    28
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    What results do you get? Also, did the tutorial give you the code, or suggest that you try it out? If you want to learn to manually sort that's fine, I would like to point out that you can call the static arrays method Arrays.sort.
    thanks I'm just going back to basics to get a better understanding of arrays
    Last edited by leoshiner; 05-01-2011 at 01:22 PM. Reason: mistake

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Alright, don't be afraid to come here with any help you may need, the people here are very helpful. If you are done with this thread please mark it solved with the thread tools at the top of the page.

  7. #7
    leoshiner is offline Member
    Join Date
    Oct 2010
    Posts
    28
    Rep Power
    0

Similar Threads

  1. ascending order using array rush
    By jca in forum New To Java
    Replies: 2
    Last Post: 01-03-2011, 05:24 AM
  2. Replies: 3
    Last Post: 11-11-2010, 11:22 AM
  3. Checking ascending order of array
    By counterfox in forum New To Java
    Replies: 3
    Last Post: 10-22-2010, 11:44 PM
  4. Sort in Cyrilic order
    By cselic in forum Advanced Java
    Replies: 4
    Last Post: 04-21-2010, 03:03 PM
  5. How to sort array objects into alphabetical order...
    By lisalala in forum New To Java
    Replies: 5
    Last Post: 03-03-2009, 12:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •