Results 1 to 7 of 7
- 05-01-2011, 01:01 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 28
- Rep Power
- 0
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
- 05-01-2011, 01:09 PM #2
Senior Member
- Join Date
- Sep 2008
- Location
- Stockholm, Sweden
- Posts
- 119
- Rep Power
- 0
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])
- 05-01-2011, 01:10 PM #3
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,068
- Blog Entries
- 3
- Rep Power
- 15
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.
- 05-01-2011, 01:18 PM #4
Member
- Join Date
- Oct 2010
- Posts
- 28
- Rep Power
- 0
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
- 05-01-2011, 01:22 PM #5
Member
- Join Date
- Oct 2010
- Posts
- 28
- Rep Power
- 0
- 05-01-2011, 01:24 PM #6
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,068
- Blog Entries
- 3
- Rep Power
- 15
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.
- 05-01-2011, 01:28 PM #7
Member
- Join Date
- Oct 2010
- Posts
- 28
- Rep Power
- 0
Similar Threads
-
ascending order using array rush
By jca in forum New To JavaReplies: 2Last Post: 01-03-2011, 05:24 AM -
Storing all file names and paths in a directory into an array
By lief480 in forum Advanced JavaReplies: 3Last Post: 11-11-2010, 11:22 AM -
Checking ascending order of array
By counterfox in forum New To JavaReplies: 3Last Post: 10-22-2010, 11:44 PM -
Sort in Cyrilic order
By cselic in forum Advanced JavaReplies: 4Last Post: 04-21-2010, 03:03 PM -
How to sort array objects into alphabetical order...
By lisalala in forum New To JavaReplies: 5Last Post: 03-03-2009, 12:01 PM
Bookmarks