Results 1 to 6 of 6
  1. #1
    cjgarcia is offline Member
    Join Date
    Apr 2012
    Posts
    8
    Rep Power
    0

    Default 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;
    	}

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default 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?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    cjgarcia is offline Member
    Join Date
    Apr 2012
    Posts
    8
    Rep Power
    0

    Default 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;
    	}			
    }

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,528
    Blog Entries
    7
    Rep Power
    20

    Default 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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    cjgarcia is offline Member
    Join Date
    Apr 2012
    Posts
    8
    Rep Power
    0

    Default 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])
    I get a binary error cause its comparing integers and not strings
    error: bad operand types for binary operator '<'
    Last edited by cjgarcia; 05-02-2012 at 09:57 PM.

  6. #6
    cjgarcia is offline Member
    Join Date
    Apr 2012
    Posts
    8
    Rep Power
    0

    Default Re: I need help sorting a array of strings in abc from a file

    nvm i fix it

Similar Threads

  1. Replies: 5
    Last Post: 07-11-2011, 01:40 PM
  2. Generate and Save Array of Strings to external file
    By thestarncy in forum New To Java
    Replies: 1
    Last Post: 03-05-2010, 04:46 PM
  3. Sorting strings in java?
    By alexander.s in forum New To Java
    Replies: 10
    Last Post: 09-01-2008, 04:14 AM
  4. Sorting an array of Strings
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-15-2008, 07:39 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
  •