• 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
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

When sorting Array, the array elements become identical to what the user inputted

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm trying to alphanumerically sort a plate number (which is a string comprised of an alternating uppercase letter and digit) into an already sorted array. I'm; however, getting an array that has the same plate number as the one I'm trying to check.
Example: Plate Number to be sorted: U7I0L1
Plate Number List: A8M2M2, F4F1N4, N3N0G9
New List after using this code: U7I0L1 U7I0L1 U7I0L1

How it should be:  A8M2M2, F4F1N4, N3N0G9,  U7I0L1


                   

                   
 
Saloon Keeper
Posts: 8577
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to create and use a 'swap' method.
For comparing Strings use String#compareTo().
 
Marshal
Posts: 73984
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What does that code with all the } } } } } do? I am sure there will be much easier ways to do that. It looks so complicated that it could hide all sorts of errors without your being aware of them.
Are you sorting an array or inserting an element at a particular index? I have already made suggestions about insertion.
 
Greenhorn
Posts: 8
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My answer assumes you want to practice coding 'from scratch' and implement your own algorithm.

I think you are starting with an array of Strings (sorted)  with length 3 and then you are trying to produce an array of length 4 with one element having been inserted lexocographically.

This type of array cannot 'grow'  to permit the insert you desire but you could copy the four elements to a different array.  ( Or use an ArrayList , which can grow.)

It also looks like you are trying to write from scratch a lexicographic method simply to determine which  string is greater. For that you might iterate  over the two strings being compared: the    charAt and an > operator  could compare  each character that you inspect.

       

The above code compares chars at index 0 and then index 1 but you would  instead want a for loop to keep going, looking for the first character which is less, perhaps.



This will get you started and get rid of most the if statements in your code.
 
Campbell Ritchie
Marshal
Posts: 73984
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your method will work nicely for Strings with so‑called asciibetical ordering, a jocular name for the same ordering as String's compareTo(String) method. Note there are other ways to compare Strings which you can find from the documentation. But your method can go wrong if the two Strings are different lengths. It would be a good idea to stop the loop as soon as a difference is found.
I am not quite sure what the idea behind this question is, whether it is necessary to write sorting algorithms or whether one can use ready‑made methods. I suspect that no sorting is actually required because the array is provided already sorted. I am not sure how the loop is supposed to work, but the many assignments in it look dubious to me. Look at lines 4, 5, 6. That sort of assignment is repeated throughout that loop. Please go through those assignments and work out what they will do to the array. I think that loop is beyond repair and you will have to start again. If you really need to sort arrays, writing a swap method, as Carey says, would be a good starting point.

I think this question is related that that here.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic