Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

ArrayList selection Sort

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey there,
I created an ArrayList called names. The idea is that the user enters a bunch of names in any order and the program sorts them and prints a report sheet with the names sorted. Here is what I have for my sort method so far.

I get an error telling me that I cannot use > on the line

I realize this is probably some simple error but i don't understand why i cannot do that or how to get around it... help please....
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bryan

I'm not too sure what you are trying to use the '>' operator for.
that operator works for numerics, not string values.

what you can do is




But the best solution is to use the Collections class



This will sort the list by its natural ordering which should be alphabetical
 
Bryan Peach
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok so now I have my sort method and a swap method... the sort method is supposed to figure out the order of where the strings should be... and my swap method is supposed to swap the contents in the string list i have PLUS make the same changes in two other lists i have made to keep the data compatible... here is the code for the two methods.

I get a few errors from these when trying to compile. First of all i get a error from the method call within the sort method saying it cannot find the symbol variable someSalesList. but thats the call i used all throughout the program for my methods and it worked... do you have to use a different call when calling a method from within a method?
The other errors i get are cannot find symbol method set in my swap method... any ideas?
Also, this sorting and swapping thing is very new to me... and while i understand the concept and how it is supposed to work the code seems very confusing to me... does the code look alright? or are there some serious problems going on... Thanks
 
Marshal
Posts: 79645
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not quite alphabetical, but what is jocularly called asciibetical. More details here, as well as the Google search earlier.

Note many of these questions are class exercises where using Collections#sort is prohibited.
 
Campbell Ritchie
Marshal
Posts: 79645
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You ought to use a static swapList method which takes the List, and the two indices as its parameters.
  • Use int rather than Integer.
  • Use i as your index in the for loop, not l because l can be difficult to distinguish from 1.
  • In a selection sort, you will probably want to start the loop with max = k;
  • It looks like a very awkward way to arrange sorting, trying to sort three Lists in parallel. Very difficult to maintain. You want a List of SalesAndExpenses and a SalesAndExpenses object which incorporates the three data you are using.
  • Maybe a different name for the SalesAndExpenses class would be appropriate.
  • Haev you looked through the List interface for set methods? Note how many parameters set requires.
  •  
    Rancher
    Posts: 600
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    I get a few errors from these when trying to compile. First of all i get a error from the method call within the sort method saying it cannot find the symbol variable someSalesList. but thats the call i used all throughout the program for my methods and it worked... do you have to use a different call when calling a method from within a method?



    Did you define someSalesList in your sortList() method? Is it an instance variable of your class?

    John.
     
    Bryan Peach
    Ranch Hand
    Posts: 76
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    John, I defined someSalesList in the main method and thats what i used to call all my other methods and it worked. So i'm not sure if there is a different way to call it from within a method. Here is the entire code. That might help to see what is going on here.
     
    Campbell Ritchie
    Marshal
    Posts: 79645
    380
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Do a search for my posts about Scanner. You will not get "Peach Bryan" by using the next() method; you will get "Peach", followed by an Exception when it tries to turn Bryan in to an int. You probably want "nextLine".
    Your totalExpenses will give the wrong result; you don't want a field at all, but a local variable, otherwise you get more expenses every tiem that method is called.

    You still have the design problem that you don't have a class which incorporates all the data. I now think, having see what you are doing, that a Salesman (or Saleswoman) class is what you want rather than SalesAndExpenses. It will probably also prevent you from trying to add a String to a List<Integer>.
     
    John de Michele
    Rancher
    Posts: 600
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Bryan:

    It works in your main() method because someSalesList is in scope in your main() method. It doesn't work in your sortList() method because someSalesList is not in scope there. You need to either a) define someSalesList in the sortList() method, or (better) b) pass someSalesList to sortList(), so that it can work on it.

    John.
     
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic