Results 1 to 6 of 6
- 05-02-2012, 10:31 PM #1
Member
- Join Date
- Apr 2012
- Posts
- 8
- Rep Power
- 0
I need help sorting a array of strings in abc from a file
This is the code that my teacher provided us with to use in our program. He said we just now need to modify it and change it so it can handle strings instead of integers. I already have the sort() working the way it needs to be. The problem is the findSmallestLoc method. How do I compare the strings of the array that is in a file to go into abc order?
Java Code://Sorts the array to make sure its in abc order static void sort (Word list [], int num) { Word temp; int position; for (int loop = 0; loop < num - 1; loop++) { position = findSmallestLoc (list, loop, num - 1); if (position != loop) { temp = list[position]; list[position] = list[loop]; list[loop] = temp; } } }
Java Code:static int findSmallestLoc (Word list[], int start, int stop) { int loc = start; for (int pos = start + 1; pos <= stop; pos++) //right here is where I need help to be more specific if (list [pos] < list [loc]) loc = pos; return loc; }
- 05-02-2012, 10:40 PM #2
Re: I need help sorting a array of strings in abc from a file
Have you gone through the API for String? Did you find any method that could be useful in comparing Strings?
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
- 05-02-2012, 10:45 PM #3
Member
- Join Date
- Apr 2012
- Posts
- 8
- Rep Power
- 0
Re: I need help sorting a array of strings in abc from a file
I know I have to use the compareTo method. What I'm more confuse on is getting the string out of the array to then compare it to the other strings in the array.
This is what I have now but I dont know if it's right.
Java Code:static int findSmallestLoc (Word list[], int start, int stop) { int loc = start; String first = ""; String last = ""; for (int i = 0; i < list.length - 1; i++) { first = list[i]; last = list[i+1]; for (int pos = start + 1; pos <= stop; pos++) if (first.compareTo(last)) loc = pos; return loc; } }
- 05-02-2012, 10:50 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Re: I need help sorting a array of strings in abc from a file
Does your code compile? I bet it doesn't because the compareTo( ... ) method returns an int, not a boolean value. Read the API description for that method again. Oh, and your indentation stinks; fix that.
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
- 05-02-2012, 10:54 PM #5
Member
- Join Date
- Apr 2012
- Posts
- 8
- Rep Power
- 0
Re: I need help sorting a array of strings in abc from a file
Well in the original post right here
Java Code:if (list [pos] < list [loc])
error: bad operand types for binary operator '<'Last edited by cjgarcia; 05-02-2012 at 10:57 PM.
- 05-03-2012, 01:21 AM #6
Member
- Join Date
- Apr 2012
- Posts
- 8
- Rep Power
- 0
Similar Threads
-
Help with algorithm for sorting an array of Strings
By eIO in forum New To JavaReplies: 5Last Post: 07-11-2011, 02:40 PM -
Generate and Save Array of Strings to external file
By thestarncy in forum New To JavaReplies: 1Last Post: 03-05-2010, 05:46 PM -
Sorting strings in java?
By alexander.s in forum New To JavaReplies: 10Last Post: 09-01-2008, 05:14 AM -
Sorting an array of Strings
By Java Tip in forum java.langReplies: 0Last Post: 04-15-2008, 08:39 PM
Bookmarks