• 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
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

Change the first letter

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want a method that makes the first letter of  word capitalized. The code works, with the execution of the first letter of the first word.
asdsa Sad     Asdas Asd Sad
 
Marshal
Posts: 72926
330
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The String object has lots of methods which will make your task much easier. Split the String on spaces. Take a substring length 1 and convert it to upper case; then recombine it with the remainder of the original word. Use a StringJoiner to join all your Strings back into one.
 
Ranch Foreman
Posts: 415
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What Campbell said.  The first try looks something like I might have coded in C at the beginning of the 90's, but even there the standard library had several helpful methods.

You definitely want to leverage the standard string and character functions that are heavily tested, optimized, and well-recognized by other Java programmers.

Additionally if you ever find yourself writing:


anywhere, you should ask yourself "Why?" as Java (unlike Python) has character literals and variables as built-in types:


or just write ' ' in the source code.

That jumped out at me, because Python decided to not have a built-in char type, it is just a str that happens to be one character.

But in Java we have single char variables and literals and you should feel free to use them where appropriate.
 
Saloon Keeper
Posts: 8222
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
What's a "word"? Here's the input and two outputs. Which is correct?
 
Jesse Silverman
Ranch Foreman
Posts: 415
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Character class also has some very helpful selections in it:
https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Character.html

I don't know if working with a-zA-Z only was part of the spec, but the Character class has methods that are quite Unicode sensitive, altho there are still some tricks surrounding languages that contain languages that have lower case letters without upper case counterparts or vice versa.  There's some pretty weird written languages out there.

 
Marshal
Posts: 26601
81
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

Jesse Silverman wrote:I don't know if working with a-zA-Z only was part of the spec, but the Character class has methods that are quite Unicode sensitive...



Have a look at ß for an example of that. It used to be that the upper-case version of that character was "SS", which of course can't be implemented by the Character class. However, reading that extremely comprehensive Wikipedia article I see that an upper-case version is now officially part of Unicode and has been officially accepted in German for just a few years.

So I don't know how it's treated in Java these days; new Java versions traditionally incorporate new versions of Unicode as a matter of course, so that treatment may well have changed over the last few years.
 
Campbell Ritchie
Marshal
Posts: 72926
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JShell doesn't seem to find a capital form of ß I think there is a mistake in that Wikipedia article. It says that ẞ is only used in German, but I have seen it in copies of old French documents. It is no longer used in French.

String s = "\u1e9e";
s ==> "ẞ"

jshell> s.toUpperCase()
$2 ==> "ẞ"

jshell> System.out.printf("%#x%n", +s.toUpperCase().charAt(0));
0x1e9e

0x1e9e: same as it was before. Note the plus sign is the rarely‑used promotion operator, not addition nor concatenation. I got the same result with Character.toUpperCase().
 
Jesse Silverman
Ranch Foreman
Posts: 415
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The point was that with only a few exceptions due to not every character having a different case counterpart in a given language, the Character functions work just great.

Whether that particular character is one of the at a particular Unicode release and some particular Java version that supports that Unicode version is a detail that may vary.

Of course, there are languages that have no concept of upper and lower case at all, for those these functions are no-ops, I believe, rather than throwing an exception or error.
 
Jesse Silverman
Ranch Foreman
Posts: 415
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Note the plus sign is the rarely‑used promotion operator, not addition nor concatenation. I got the same result with Character.toUpperCase().



You mean converting Java's notion of a char as an unsigned 16-bit value to an int?
 
Campbell Ritchie
Marshal
Posts: 72926
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes.
 
Master Rancher
Posts: 3889
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:JShell doesn't seem to find a capital form of ß I think there is a mistake in that Wikipedia article. It says that ẞ is only used in German, but I have seen it in copies of old French documents. It is no longer used in French.


The article does show an example of this, as well as an example using Latin.  I believe they mean it is currently only used in German, which is accurate.
 
Campbell Ritchie
Marshal
Posts: 72926
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, currently used in German is correct, but that isn't what the article said.
As Homer Simpson would say, D'oh! I looked up 1e9e on Unicode and discovered it already is the upper‑case equivalent. Not that I have ever seen that upper‑case letter in real life. I have never seen a German word starting with ẞ/ß or double S. Character.toUpperCase() doesn't seem to recognise 1e9e:-

jshell> char sz = 0x00df;
sz ==> 'ß'

jshell> System.out.printf("%c to upper case = %#x%n", sz, +Character.toUpperCase(sz));
ß to upper case = 0xdf
$2 ==> java.io.PrintStream@723279cf

Maybe it will recognise 1e9e in future versions.
 
Jesse Silverman
Ranch Foreman
Posts: 415
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Yes, currently used in German is correct, but that isn't what the article said.
As Homer Simpson would say, D'oh! I looked up 1e9e on Unicode and discovered it already is the upper‑case equivalent. Not that I have ever seen that upper‑case letter in real life. I have never seen a German word starting with ẞ/ß or double S. Character.toUpperCase() doesn't seem to recognise 1e9e:-

jshell> char sz = 0x00df;
sz ==> 'ß'

jshell> System.out.printf("%c to upper case = %#x%n", sz, +Character.toUpperCase(sz));
ß to upper case = 0xdf
$2 ==> java.io.PrintStream@723279cf

Maybe it will recognise 1e9e in future versions.



I was thinking that some of these would only be in upper-case when we DECIDE TO SHOUT SOME SEQUENCE OF WORDS.

Germans are often too polite to do that, but it is still a thing.

Fortunately we don't see too much of that here, but that was the use case I was thinking of.
 
Mike Simmons
Master Rancher
Posts: 3889
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesse Silverman wrote:I was thinking that some of these would only be in upper-case when we DECIDE TO SHOUT SOME SEQUENCE OF WORDS.

Germans are often too polite to do that, but it is still a thing.



But then, historically...
 
Bartender
Posts: 2864
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@OP if you are curious as to why your program is not working for the first alphabet, Look at your code:This code states that if the current character is a space and current index "i" is less than the length of the word, than do something. This is not the case with your string: "asdsa sad     asdas asd sad" as it does not begin with space.
Having said that, Campbell's suggestion is simple (if you are not constrained to A-Z only)
reply
    Bookmark Topic Watch Topic
  • New Topic