• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

sorting with capitals & lower case

 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello again. How long does it take to get used to the house with the smoking chimney with the "I agree" at the bottom of every post?

I'm supposed to write a program that removes the spaces & punctuation in a string & then puts the letters in alphabetical order. The program below does that, but it puts all the capital letters first. I understand why it does that, but I can't figure out how to make it not do that. I thought there might be a way to create a second character array just for the capital letters, but I don't know how I'd get them back in the right places. Can anyone help? Thanks!


 
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

For the house, as for the moose, I hadn't been here for 15 years and I could swear they are the same that were there in 2000 so you should get used to it eventually ;-)

I am positive there are other ways but you could define your own Comparator and pass it to the Arrays.sort method

Arrays.sort(characters,YOUR_OWN_COMPARATOR);

You could just convert all chars to lower case in the comparator.

I posted a Comparator example in another thread not related to this topic:

https://coderanch.com/t/656499/java/java/ways-initialize-array#3040684
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Karen Guffey,
Try this program I'm giving. You'll get what you want. I've used array concept. Instead if we use ArrayList concept, lines of code will be less.

Edited to remove code. We don't approve of handing out complete code as answers to questions here, as that isn't a good way for the posters to learn anything. (Paul C - moderator)
 
Marshal
Posts: 76825
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rather than using chars use full‑blown objects. Rather than a char[] try a String[] where each String contains a single character. Hint: try the substring method. Then get yousrelf a Comparator to sort the array with. Hint: there is a ready‑made Comparator in the String class. Look through its documentation and you will find it. Then append each element in your array to a StringBuilder object. You can check whether it matches \\W when you get the input from the keyboard, or at the stage when you append to the StringBuilder.
Then you can convert the StringBuilder back to a String with its toString method.
 
A.J. Côté
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Rather than using chars use full‑blown objects. Rather than a char[] try a String[] where each String contains a single character..



Hi Campbell,

I am not sure this is advisable performance-wise. In java, strings have an overhead and that's why we made them final to compensate a bit. char[] should be more efficient.

Just my 2 cents.
 
Campbell Ritchie
Marshal
Posts: 76825
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And how much performance overhead will there be compared to entering the text via the keyboard?
 
Campbell Ritchie
Marshal
Posts: 76825
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Strings were made final so they would be immutable so they can be shared or interned. At least whoever wrote the String class realised immutability was desirable. We get on to Java8 and only now do we find lots of immutable classes being written.
 
Saloon Keeper
Posts: 26268
186
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Apologies for what are undoubtedly major syntactical errors, since this is off the cuff. Also, if I'm not mistaken, recent JVMs can use a lambda for the Comparator.

No attempts have been made to make this robust. For example, I didn't add logic to handle null Character objects.

The String/Comparator sort is actually quite important, since Strings can be interpreted in many different ways. In EBCDIC, the natural collation of digits is greater than it is for characters (the opposite of ASCII) and there are some special characters whose natural collation is actually in gaps between the letter codes. Then there's the whole thing about sorting accented letters and other decorations. Separately or as a unit? That's even before you get to some of the real challenges. "ll" used to collate after "l" in Spanish dictionaries, but I believe these days it collates like 2 "l"s. Or the conventions such as "OE" instead of "Ö" in German or "n~" instead of "ñ" in spanish (we had a mainframe data feed drive us nuts with that one!)

Such fun!
 
Campbell Ritchie
Marshal
Posts: 76825
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That equals() method won't compile because Comparator only has an equals() method which is override‑equivalent to that in java.lang.Object. What follows will only work in Java8:-It will not work at all if I have got the syntax wrong
 
Sheriff
Posts: 7123
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I couldn't get Campbell's code to compile. This is as close as I got:


Notice that you need Character[]. char[] won't work.
 
Campbell Ritchie
Marshal
Posts: 76825
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Of course. The array sorted has to be a subtype of Object[]. Primitive arrays are not suitable.
 
Marshal
Posts: 27450
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Writing your own code to do case-insensitive comparison of Strings is all very well, and probably has some instructional value, but for real-life programming you'd just go into the java.util package and use Collator.TERTIARY for a Comparator which does that.
 
Karen Guffey
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, guys. This was actually way way WAY over my head, because comparators are in chapter 22 of my book, & we're in chapter 9. But I was able to find a lot of examples & piece together a program that works. I have no idea of what half of it is doing, but I suppose I'll get there eventually, if I don't run away from Java altogether after this course is over.

Thank you very much. Classes & objects are the next chapter!
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Rather than using chars use full‑blown objects. Rather than a char[] try a String[] where each String contains a single character.


Or alternatively, create a modifiable List class that wraps a char[], eg:and sort it
The advantage of that approach being that you can then create a Comparator<Character> to order the characters any way you like.

@Karen: The 'Abstract...' collection classes are well worth getting to know, because they provide building blocks for some very powerful "views".

Yet another approach, if you're on version 8, would be to use streams; but the sorting options are limited (and different from the way String orders).

As you can see - and as often happens in programming - there are many ways to do this; and none of them is "right".
Some might be a bit "righter" than others though.

HIH

Winston
 
Saloon Keeper
Posts: 14490
325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:Writing your own code to do case-insensitive comparison of Strings is all very well, and probably has some instructional value, but for real-life programming you'd just go into the java.util package and use Collator.TERTIARY for a Comparator which does that.



Surely that should be Collator.SECONDARY?

Anyway, I didn't even know about the Collator class. Awesome!
 
Campbell Ritchie
Marshal
Posts: 76825
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please show us what you have got.
 
Karen Guffey
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is my solution. Actually, mine & those of some other posters in Java places. I understand relatively little of the parts that I pretty much just imitated. I understand what most of the pieces are but not necessarily how they function. I tell you all this so that you don't think I know more than I actually do!

 
Knute Snortum
Sheriff
Posts: 7123
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Two comments in the code are incorrect.


Rather, this should say something like, "Remove all characters except alphabetic and underline."


This creates an array of Character, which is a wrapper for the primitive char. It is not a String, but it is an Object.
 
Karen Guffey
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Argh--I knew that it was objects & not strings! Thank you, Knute!
 
Knute Snortum
Sheriff
Posts: 7123
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another thing (there may be more but I like to do one thing at a time), why not downcase the entire String manipulations? Then you know all your characters will be lowercase and you don't have to use the complex Comparator.
 
Karen Guffey
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But I need the final output to have the same letters, capital & lower case, that the user input had. If I lowercase the string, there's no way to make the capitals into capitals again, is there?
 
Campbell Ritchie
Marshal
Posts: 76825
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Enter a phrase:
Mary had a little lamb; its fleece was white as snow.
aaaaaabcdeeeeefhhiiillllMmnorssssttttwwwy

 
Karen Guffey
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Campbell: ?
 
Campbell Ritchie
Marshal
Posts: 76825
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, it worked.
 
Karen Guffey
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh! Thank you. But it's like driving a car. I can get in & make it go, but I have little idea of why it goes. This particular problem, not all of them. But since what my course teaches & what it tests over are becoming more & more removed from each other, I just want to get through the course & then go back to a MOOC that actually allows me to practice what it teaches as I go along.
 
straws are for suckers. tiny ads are for attractive people.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic